Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

on Named arguments

rahzan
New Contributor I
989 Views
For an optional dummy variable one can use explicit names:

call sub(x,A=1,C=2)
...
subroutine sub(x,A,B,C)
real,optional:: A,B,B
real x
...

Is there a way to force the caller to use the named method?

Thanks,
Tim H
0 Kudos
7 Replies
Steven_L_Intel1
Employee
989 Views
Not that I can think of.

Steve
0 Kudos
Jugoslav_Dujic
Valued Contributor II
989 Views
Here's a practical one:
type RS4UU9OPOM98GHEJ390O0O0O
end type RS4UU9OPOM98GHEJ390O0O0O

subroutine sub(x,OO00UI764IOPLELMS89OQA,A,B,C)
real:: x
type(RS4UU9OPOM98GHEJ390O0O0O), optional:: OO00UI764IOPLELMS89OQA
real, optional:: a, b, c
...
See the point? :-)))))))

Jugoslav
0 Kudos
Steven_L_Intel1
Employee
989 Views
Oh, that's diabolical!

Steve
0 Kudos
rahzan
New Contributor I
989 Views
DUHHHH!
Is an empty type even allowed. Guess I'm missing it entirely!!!

Slowly yours,
Dumbo
0 Kudos
Steven_L_Intel1
Employee
989 Views
No, actually. But you could insert some dummy component.

Steve
0 Kudos
Intel_C_Intel
Employee
989 Views
Please correct me if I'm wrong, but....

In Jugoslav's example, he uses the fact that any ambiguity among optional arguments forces the caller to be explicit. A caller is extremely unlikely to define a variable of that excruciating type (I can't bear to type the name), therefore, they must explicitly name the remaining arguments.

As you had asked, the result is that the user must always call the routine with the arguments explicitly named.
0 Kudos
Steven_L_Intel1
Employee
989 Views
It's not ambiguity, per se. Jugoslav's technique makes it difficult for the programmer to pass the second argument - they would have to declare a variable of the proper type. Note, however, that the type IS visible to the caller - it has to be - so if the programmer was determined, they could do it.

Since the second argument is optional, it may be omitted. But if it is omitted, the language requires that all following arguments be passed using the keyword name. Also, CVF disallows using consecutive commas (an extension) when calling a routine that has an explicit interface.

Steve
0 Kudos
Reply