Re: Statement functions
- From: Joe Krahn <krahn@xxxxxxxxxxxxx>
- Date: Mon, 20 Mar 2006 17:37:42 -0500
James Giles wrote:
Joe Krahn wrote:
Just another minor feature suggestion:
Why not include an optional "FUNCTION" keyword at the beginning of
function statements? It would be a simple way to avoid ambiguity with
array assignment, especially in the case of an internal subprogram.
It would have to be "FUNCTION ::". Otherwise you wouldn't
be able to tell the difference in fixed form. It isn't so much an
ambiguity as it is easily susceptible to error. If the host (or
a module you failed to say ONLY with) has an array of the
same name and you forget to give a local type declaration
for it, the lisn *is* an assignment. This is not worse than
(and less common than) other instances where an intended
local entity has a different meaning than desired because
the programmer failed to make the local declaration and
accidentally got something from outside instead.
However, the thing you recommend would be useful in another
way. It would identify the thing as a statement function without
specifying a type. The rule could be that statement functions
declared that way were completely generic. So:
Function :: double(a) = a + a
This function would be defined on any type for which the plus
operator as defined. And, it would return the type that the plus
operator on that type of argument would notmally return. In
this case, not only is a separate declaration of the type of DOUBLE
not required, it's not permitted. And, any separate declaration
of A is irrelevant since in the above, A is a dummy argument
whose scope is for that single statement only.
Note, when I say that the function is completely generic, it
means that even if the argument type is one that doesn't have
an intrinsic plus operator, if there's an overload for that type,
ít's still meaningful to use the statement function.
(I've been thinking of using the keyword "STMT :: ".)
So, I guess the only advantage of a "FUNCTION ::" prefix is code readability. Of course, it could be even more readable if the type declaration and function were combined:
REAL, FUNCTION :: x(y) = ...
Or, for your idea of the generic form:
GENERIC, FUNCTION :: x(y) = ...
or, because the result is implicit:
IMPLICIT, FUNCTION :: x(y) = ...
Unlike regular functions, a statement-function return type can be easily defined as the type you would get from evaluating the expression made from substituting the arguments. Essentially, it's the same effect as a macro.
As for the keyword STMT, I'm sure that STATEMENT is a better Fortran-istic choice.
Joe
.
- Follow-Ups:
- Re: Statement functions
- From: James Giles
- Re: Statement functions
- References:
- Statement functions
- From: Joe Krahn
- Re: Statement functions
- From: James Giles
- Statement functions
- Prev by Date: Re: Statement functions
- Next by Date: Re: Why stack overflow here?
- Previous by thread: Re: Statement functions
- Next by thread: Re: Statement functions
- Index(es):
Relevant Pages
|