I'm trying to return a type from a fortran function. This is the code.
module somemodule
implicit none
! define a simple type
type sometype
integer :: someint
end type sometype
! define an interface
interface
! define a function that returns the previously defined type
type(sometype) function somefunction()
end function somefunction
end interface
contains
end module somemodule
In gfortran (4.4 & 4.5) I get the following error:
Error: The type for function 'somefunction' at (1) is not accessible
I compiled the file as:
gfortran -c ./test.F90
I tried to make the type explicitly public but that didn't help. I was planning to use a c version of the somefunction, that is why I put it in the interface section.
Why is the type not accessible?
Adding import inside the function definition fixes this. Due to what many consider a mistake in the design of the language, definitions aren't inherited inside of an interface. The "import" overrides this to achieve the sensible behavior.
The answer to the question why it is not accessible is that the standard committee designed it like that. The interface has a separate scope from the enclosing module, so you have to explicitly import names from it. Obviously(?) you can't
use
the module inside itself, so theimport
statement is needed.