module m1
interface generic
module procedure func
end interface
contains
integer function func(n)
integer, intent(in) :: n
func = n
end function
end module
module m2
interface generic
module procedure subr
end interface
contains
subroutine subr
print *, 'ok'
end subroutine
end module
program main
use m1
use m2
call generic
end
Generic interfaces are constrained in some specific circumstances to contain all functions or all subroutines, but in general they can contain both. (There's language in the standard that pertains to the visibility of intrinsic procedures of the samename that depends on whether a generic interface contains a mixture or not, so it must be possible.)
While it's probably not the best practice to explicitly define an interface that has a mixture of functions and subroutines, it's possible for such an interface to be created -- perhaps inadvertently -- when an interface is defined in multiplelocations and merged together. For example:
their dummy arguments. So I think I'm going to get to add another item to f18's documentation section on features that it supports that are standard but might as well not be.module m1
interface generic
module procedure func
end interface
contains
integer function func(n)
integer, intent(in) :: n
func = n
end function
end module
module m2
interface generic
module procedure subr
end interface
contains
subroutine subr
print *, 'ok'
end subroutine
end module
program main
use m1
use m2
call generic
end
Some compilers can handle this example, some can't, and complain that a mixed generic interface is not allowed. I think that those compilers are incorrect. If you disagree, please let me know why.
Amusingly, if you delete the function's dummy argument from that example, *no* compiler can deal with it, since they apparently don't take the function/subroutine distinction into account when comparing specific procedures for distinguishability, only
On Tuesday, November 8, 2022 at 6:16:33 PM UTC+1, Peter Klausler US wrote:name that depends on whether a generic interface contains a mixture or not, so it must be possible.)
Generic interfaces are constrained in some specific circumstances to contain all functions or all subroutines, but in general they can contain both. (There's language in the standard that pertains to the visibility of intrinsic procedures of the same
locations and merged together. For example:
While it's probably not the best practice to explicitly define an interface that has a mixture of functions and subroutines, it's possible for such an interface to be created -- perhaps inadvertently -- when an interface is defined in multiple
their dummy arguments. So I think I'm going to get to add another item to f18's documentation section on features that it supports that are standard but might as well not be.
module m1
interface generic
module procedure func
end interface
contains
integer function func(n)
integer, intent(in) :: n
func = n
end function
end module
module m2
interface generic
module procedure subr
end interface
contains
subroutine subr
print *, 'ok'
end subroutine
end module
program main
use m1
use m2
call generic
end
Some compilers can handle this example, some can't, and complain that a mixed generic interface is not allowed. I think that those compilers are incorrect. If you disagree, please let me know why.
Amusingly, if you delete the function's dummy argument from that example, *no* compiler can deal with it, since they apparently don't take the function/subroutine distinction into account when comparing specific procedures for distinguishability, only
If you take away the argument from the function, then its interface is not distinguishable from the subroutine according to ifort. With the argument, it emits a warning, but builds the program.
gfortran does not accept it on the grounds of it being a mixture, with or without the argument n.
I do not know which one is right, but the mixups (pun not entirely intended) you sketch are unfortunate and possible. Renaming would solve it, but that is besides the point.
The key is 15.4.3.4.5 Restrictions on generic declarations, where there
is a constraint:
C1514 Within the scope of a generic name, each pair of procedures
identified by that name shall both be subroutines or both be
functions, and [...]
I think this makes the code illegal, and not diagnosing it is a bug.
Please feel free to disagree :-)
Generic interfaces are constrained in some specific circumstances to contain all functions or all subroutines, but in general they can contain both. (There's language in the standard that pertains to the visibility of intrinsic procedures of the samename that depends on whether a generic interface contains a mixture or not, so it must be possible.)
While it's probably not the best practice to explicitly define an interface that has a mixture of functions and subroutines, it's possible for such an interface to be created -- perhaps inadvertently -- when an interface is defined in multiplelocations and merged together. For example:
.. So I think I'm going to get to add another item to f18's documentation section on features that it supports that are standard but might as well not be.
On Tuesday, November 8, 2022 at 12:16:33 PM UTC-5, Peter Klausler US wrote:name that depends on whether a generic interface contains a mixture or not, so it must be possible.)
Generic interfaces are constrained in some specific circumstances to contain all functions or all subroutines, but in general they can contain both. (There's language in the standard that pertains to the visibility of intrinsic procedures of the same
Re: "but in general they can contain both," from what I can understand from 18-007r1 document toward Fortran 2018, I do *not* think the semantics of the standard is such that "in general they can contain both" Sure the standard has following carve outbut that does suggest any generality
On Thursday, November 10, 2022 at 1:42:11 PM UTC-8, Thomas Koenig wrote:
The key is 15.4.3.4.5 Restrictions on generic declarations, where there
is a constraint:
C1514 Within the scope of a generic name, each pair of procedures
identified by that name shall both be subroutines or both be
functions, and [...]
I think this makes the code illegal, and not diagnosing it is a bug.
Please feel free to disagree :-)
I think that in my example above the "scope of the generic name"
is one module or the other, but in the main program the generic
name is being accessed via USE association and the main program
is technically not in a (or the) scope of the generic name.
I disagree there. Both interfacs are accessed by use association
in the main program, and thus are in its scope.
At least this is how I interpret use association and 14.2.2.
Thanks for the response. You're probably right. But since at least four compilers support this combination, so must I, and it's just a matter
of determining how it should be documented.
Generic interfaces are constrained in some specific circumstances to contain all functions or all subroutines, but in general they can contain both. (There's language in the standard that pertains to the visibility of intrinsic
procedures of the same name that depends on whether a generic
interface contains a mixture or not, so it must be possible.)
On Friday, November 11, 2022 at 2:21:37 PM UTC-8, Thomas Koenig wrote:
I disagree there. Both interfacs are accessed by use association
in the main program, and thus are in its scope.
At least this is how I interpret use association and 14.2.2.
Thanks for the response. You're probably right. But since at
least four compilers support this combination, so must I, and it's
just a matter of determining how it should be documented.
So, you can support it, and you can even hide the check behind
some options, but unless you want a bug in the compiler, you need
to be able to issue a diagnostic.
As a matter of policy, I think all constraint violations should
be errors by default, and if needed, options should be used
to downgrade the error to a warning (which is still a diagnostic)
or to quieten it entirely.
On Friday, November 11, 2022 at 2:21:37 PM UTC-8, Thomas Koenig wrote:..
I disagree there. Both interfacs are accessed by use association
in the main program, and thus are in its scope.
At least this is how I interpret use association and 14.2.2.
Thanks for the response. You're probably right. But since at least four compilers support this combination, so must I, and it's just a matter of determining how it should be documented.
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 159 |
Nodes: | 16 (0 / 16) |
Uptime: | 100:03:48 |
Calls: | 3,209 |
Files: | 10,563 |
Messages: | 3,009,979 |