Quantcast
Channel: Intel® Fortran Compiler
Viewing all articles
Browse latest Browse all 3270

Protected variables in modules with sub-modules

$
0
0
module mymod
    implicit none
    integer, protected :: n

    interface
        module subroutine set_n(i)
            integer, intent(in) :: i
        end subroutine set_n
    end interface
end module mymod

submodule(mymod) mymod_i

contains
    module subroutine set_n(i)
        integer, intent(in) :: i
	    n = i
    end subroutine set_n
end submodule mymod_i

program test
    use mymod
    implicit none
    integer :: i

    i = 5
    call set_n(i)
    print *,n
end program test

The above code fails to compile with the error

ifort tc.f90
Intel(R) Visual Fortran Intel(R) 64 Compiler for applications running on Intel(R) 64, Version 16.0.2.180 Build 20160204
Copyright (C) 1985-2016 Intel Corporation.  All rights reserved.

tc.f90(17): error #7986: A use associated object that has the PROTECTED attribute shall not appear in a variable definition context.   [N]
                n = i
----------------^
compilation aborted for tc.f90 (code 1)

Is it possible to protect module data and still use the sub-module structure? If I had written this in just a single module there would not have been a problem. Shouldn't a sub-module's procedures have the same access rights to module data as if they were part of the module?

Thanks.


Viewing all articles
Browse latest Browse all 3270

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>