Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is the type not accessible?

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?

like image 704
SiggyF Avatar asked Jan 05 '12 23:01

SiggyF


2 Answers

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.

interface
   ! define a function that returns the previously defined type
   type(sometype) function somefunction()
   import
   end function somefunction
end interface
like image 194
M. S. B. Avatar answered Nov 10 '22 19:11

M. S. B.


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 the import statement is needed.

like image 34
eriktous Avatar answered Nov 10 '22 18:11

eriktous