fortranfortran2008

Restricting access to module procedures from other module procedures


I have recently discovered the new feature of Fortran 2008, i.e., SUBMODULEs.

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

Solution

  • Not really.

    Both sub1 and sub2 are accessed by sub0, which means that either (or some combination):

    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.