Why is the type not accessible?

2019-01-26 20:40发布

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?

2条回答
叛逆
2楼-- · 2019-01-26 21:23

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
查看更多
混吃等死
3楼-- · 2019-01-26 21:26

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.

查看更多
登录 后发表回答