Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access a parameter from an interface (Fortran)

Tags:

fortran

I am using a parameter to fix the precision of the used types. This works fine until I try to use the same type within an interface. Consider this small example:

module Hello
    implicit none

    save
    integer, parameter  :: K = selected_real_kind(10)

contains

    subroutine dosomething(fun)
        real(K)     :: foo
        interface
           function fun(bar)
                real(K) :: bar
                real(K) :: fun
           end function fun
        end interface
    end subroutine

end module

Here, foo would be of the desired type, whereas the compiler (gfortran) complains for 'bar' and 'fun'.

The error is

Error: Parameter 'k' at (1) has not been declared or is a variable, which does 
not reduce to a constant expression

Is there a way to get this working? (For now, I am just writing selected_real_kind(10) everywhere but this is not elegant at all)

Thank you!

like image 360
user3517498 Avatar asked Jan 11 '23 20:01

user3517498


1 Answers

The easiest way is to add import inside the interface. It is somewhat of a misdesign that the definitions of the module are outside the scope of the interface. Plain import will import everything.

....
    subroutine dosomething(fun)
        real(K)     :: foo
        interface
           function fun(bar)
                import
                real(K) :: bar
                real(K) :: fun
           end function fun
        end interface
    end subroutine
....

Also possible: import :: K

like image 120
M. S. B. Avatar answered Mar 27 '23 03:03

M. S. B.