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

Public attribute for generic interface overloading a type name

Martin1
New Contributor I
497 Views

I was wondering, whether I need a explicit public statement in the following situation:

module mod

implicit none
private

! necessary?
!public mytype

type, public :: mytype
   integer :: i = 1
end type mytype

interface mytype
   module procedure mytype_constructor
end interface mytype

contains

   function mytype_constructor() result(x)
      type(mytype) :: x
      x%i = 2
   end function mytype_constructor

end module mod


program public_generic_type

use mod
implicit none

   type(mytype) :: x
   print *,x%i

   x = mytype(3)
   print *,x%i

   x = mytype()
   print *,x%i

end program public_generic_type

The type mytype is public. But what about the generic interface mytype? The code works as expected, i.e. the generic interface is exported as well. Ifort even complains if I uncomment the public statement for mytype. gfortran accepts both. I expect that this is well covered by the standard. But I could not find any hints in Metcalf-Reid-Cohen about this situation. Anyway, I find it a bit unsatisfactory, as it is kind of unsymmetrical to provide the public statement only for the type but not for the interface block (where it is not allowed).
 

0 Kudos
5 Replies
Juergen_R_R
Valued Contributor I
497 Views

The NAG compiler also complains in case of two public declarations of mytype. PGI and gfortran do not complain. NAG is famous for being very scrupulous in standard conformance, so possibly NAG and Intel are correct. I am travelling right now, so I cannot look up the details in the standard. 

0 Kudos
Steve_Lionel
Honored Contributor III
497 Views

An interesting question. The wording for this is scattered across several parts of the standard, but the fundamental rule is that a derived type name and its constructor (whether intrinsic or user-defined through a generic) share the same accessibility. You can't make a type public but its constructor private.

Therefore, it is not valid to specify the public attribute twice for the same name.

0 Kudos
Martin1
New Contributor I
497 Views

Thanks for looking up. Sometimes it feels like that newer fortran has far too many subtle rules. I should have taken some courses in law, instead of concentrating on functional analysis and PDEs. That would have helped more with reading and understanding the fortran standard :^)

 

0 Kudos
Juergen_R_R
Valued Contributor I
497 Views

Haha, yes, there is the notorious joke: "Programming is like writing a book, except that when you omit a comma on page 187 the darn whole think doesn't make sense any more." :D

0 Kudos
Steve_Lionel
Honored Contributor III
497 Views

There's also the notorious joke "If you write something down in two places, it will be wrong in one of them." The standard tries very hard not to repeat itself because it's often been the case where a change in one section conflicts with wording in another.

0 Kudos
Reply