I have recently discovered the new feature of Fortran 2008, i.e., SUBMODULE
s.
Please have a look at my minimum working example down the question. After compilation, it puts the following on the terminal:
Accessed sub0
Accessed sub1
Accessed sub2
That is, as it should, module procedures of sub1
and sub2
can CALL
each other and everything is OK.
Because of reasons like code architecture and maintenance, I need to restrict this access somehow. That is, module procedures (sub1
and sub2
) be invisible to each other. Can I do so?
MODULE parent
PRIVATE
PUBLIC :: sub0
INTERFACE
MODULE SUBROUTINE sub1 ()
END SUBROUTINE
MODULE SUBROUTINE sub2 ()
END SUBROUTINE
END INTERFACE
CONTAINS
SUBROUTINE sub0 ()
PRINT *, 'Accessed sub0'
CALL sub1 ()
END SUBROUTINE
END MODULE
SUBMODULE ( parent ) submod1
CONTAINS
MODULE PROCEDURE sub1
PRINT *, 'Accessed sub1'
CALL sub2 ()
END SUBROUTINE
END SUBMODULE
SUBMODULE ( parent ) submod2
CONTAINS
MODULE PROCEDURE sub2
PRINT *, 'Accessed sub2'
END PROCEDURE
END SUBMODULE
PROGRAM driver
USE parent
CALL sub0 ()
END PROGRAM
Not really.
Both sub1
and sub2
are accessed by sub0
, which means that either (or some combination):
sub
and sub2
have to be known at the same level of the module/submodule hierarchy as sub0
, as in the example. The subprograms for sub1
and sub2
have to be either at the same level as sub0
or below, in which case host association makes the knowledge of the sub1
or sub2
procedure available to the other procedure.
sub1
and sub2
need to be a public entity of some other two modules. But in this case the subprogram of sub1
or sub2
can always just directly reference the module that defines the other.
sub1
and sub2
are external procedures. Again, the subprogram of sub1
or sub2
can directly access the other external procedure.
Entities in a host can be hidden from child scopes if there is a name in the child scope that shadows the name in the host entity (or by use of the expanded capabilities of the import statement in the F2015 draft standard). You could put a dummy declaration of something with the same name as the name of the procedure that you want to block out from a particular scope, but this is rather artificial.