Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
New Contributor I
17 Views

Public attribute for generic interface overloading a type name

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
Highlighted
Valued Contributor I
17 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
Highlighted
Black Belt Retired Employee
17 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
Highlighted
New Contributor I
17 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
Highlighted
Valued Contributor I
17 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
Highlighted
Black Belt Retired Employee
17 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