After trying to compile my stats/ML lib with LFortran (version: 0.52.0
), I’m back with what looks like a limitation in LFortran’s semantic analysis:
When I call a function (with optional arguments) defined module 1
through an interface defined in module 2
, specifying optional arguments explicitly as opt=1.0
creates a problem. Passing the optional argument as simply 1.0
works, and calling the function in module 1
directly and passing the optional argument as opt=1.0
also works.
To demonstrate with a quick ~minimal example:
! module containing function
module testmod1
implicit none
private
public :: f_function
contains
elemental function f_function(x, y) result(fx)
real, intent(in) :: x
real, intent(in), optional :: y
real :: w_y
real :: fx
w_y=1.0
if (present(y)) w_y=y
fx = w_y * exp(-w_y * x)
end function f_function
end module testmod1
! module containing interface
module testmod2
use :: testmod1
implicit none
private
public :: function_interface
interface function_interface
module procedure f_function
end interface
end module testmod2
! app
program main
use :: testmod1
use :: testmod2
implicit none
print*, "1. fx is ", f_function(0.8, y=0.5)
print*, "2. fx is ", function_interface(0.8, 0.5)
print*, "3. fx is ", function_interface(0.8, y=0.5)
end program main
This compiles fine with gfortran. gfortran -o test test.f90 && ./test
gives me:
1. fx is 0.335160017
2. fx is 0.335160017
3. fx is 0.335160017
Removing the third print line and compiling/running with lfortran -o test test.f90 && ./test
gives me:
1. fx is 3.35160017e-01
2. fx is 3.35160017e-01
Leaving the third print line in and running the same gives me:
semantic error: f_function is not a function.
--> test.f90:39:24
|
39 | print*, "3. fx is ", function_interface(0.8, y=0.5)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^