From: baf on
Assuming that two arguments are passed to the sum intrinsic, how does
the compiler sort out whether the second argument is dim or mask? Does
it just look at the type (integer vs logical)?

Is it possible to do the same type of thing in Fortran (figure out which
optional argument the user is providing based on the type of the argument)?

From: Tim Prince on
On 2/24/2010 8:45 AM, baf wrote:
> Assuming that two arguments are passed to the sum intrinsic, how does
> the compiler sort out whether the second argument is dim or mask? Does
> it just look at the type (integer vs logical)?
>
> Is it possible to do the same type of thing in Fortran (figure out which
> optional argument the user is providing based on the type of the argument)?
>
I've seen expert advice posted to always specify what is wanted; it's
too error prone to rely on the standard order (dim before mask) rather
than giving the compiler more opportunties to point out errors.
You could make a generic function which selects a specific function
according to argument types, but if you have to ask whether it's
desirable, without giving an example where it clearly is, I'd say no.

--
Tim Prince
From: Richard Maine on
baf <baf(a)nowhere.com> wrote:

> Assuming that two arguments are passed to the sum intrinsic, how does
> the compiler sort out whether the second argument is dim or mask? Does
> it just look at the type (integer vs logical)?

Well, it being an intrinsic, that kind of implementation detail isn't
specified by the standard. It just has to work (and the standard writers
made sure that it could be done). That being said...

> Is it possible to do the same type of thing in Fortran (figure out which
> optional argument the user is providing based on the type of the argument)?

Sort of, except that it isn't via the "optional" argument route.
Generics distinguish by TKR (type, kind, and rank). Note that SUM is a
generic.

So you could write a generic with multiple specifics. A bit more
awkward, perhaps, than just declaring an argument optional, but yes, you
can do it.

That's sort fo the model on which that kind of intrinsic is based. But
the actual implementation of the intrinsic doesn't have to look like
that.

--
Richard Maine | Good judgment comes from experience;
email: last name at domain . net | experience comes from bad judgment.
domain: summertriangle | -- Mark Twain
From: Jim Xia on
On Feb 24, 11:45 am, baf <b...(a)nowhere.com> wrote:
> Assuming that two arguments are passed to the sum intrinsic, how does
> the compiler sort out whether the second argument is dim or mask?  Does
> it just look at the type (integer vs logical)?
>

Yes, the calls are distinguishable based on the type of the 2nd
argument. If it is an integer, it must be the DIM argument, and has
to be a scalar. If it is a logical type, then it must be a MASK, and
it has to be conformance in shape to ARRAY.

> Is it possible to do the same type of thing in Fortran (figure out which
> optional argument the user is providing based on the type of the argument)?

Yes you can do this. One way to achieve this is the following

interface sum
function sumDim (array, dim, mask)
real :: array(:)
integer :: dim
logical :: mask(:)
end function

function sumWithMask (array, mask)
real :: array(:)
logical, optional :: mask(:)
end function
end interface


Here is what the standard says about intrinsic sum


"
SUM (ARRAY, DIM [, MASK]) or SUM (ARRAY [, MASK])
"

I'd prefer the intrinsic sum though since it's more flexible than my
own generic interface :-)


Cheers,

Jim
From: baf on
On 02/24/2010 08:45 AM, baf wrote:
> Assuming that two arguments are passed to the sum intrinsic, how does
> the compiler sort out whether the second argument is dim or mask? Does
> it just look at the type (integer vs logical)?
>
> Is it possible to do the same type of thing in Fortran (figure out which
> optional argument the user is providing based on the type of the argument)?
>

Thank you all for your replies. I was aware of the generic route, but
wondered if there was some other trick to do it more cleanly.