Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ambiguous interface when using optional arguments in Gfortran

I've just stumbled across this error when compiling a bit of code that I've been using without problems for ages now. I'm using Gfortran 8.2 on Linux and I suspect that a compiler update has caused the issue.

When I define an interface with an optional argument that has a different number of non-optional arguments, Gfortran complains that the interface is ambiguous. For example, if I compile the following, I get "Ambiguous interfaces in generic interface 'test' for ‘testinit1’ at (1) and ‘testinit2’ at (2)":

module test_mod
implicit none

interface Test
    module procedure test1, test2
end interface

contains

function test1(opt) result(rslt)
    integer :: rslt
    integer, optional :: opt
    rslt = 1
end function

function test2(data, opt) result(rslt)
    integer :: rslt
    integer :: data
    integer, optional :: opt
    rslt = data
end function

end module

If I remove the optional argument opt, then it compiles fine. If I add a data argument to test1 that has a different rank to test2's data, then it compiles fine. If I add another non-optional argument to both functions, I get the same error message.

The actual code I stumbled across in is the Result interface in this file, which, as I say, used to compile as expected.

Any help appreciated!

like image 398
Sam Harrison Avatar asked Nov 19 '25 10:11

Sam Harrison


1 Answers

Gfortran complains that the interface is ambiguous

Well, that's because the interface is ambiguous. Which procedure should be chosen in the following call?

integer :: param
print *, Test(param)
  1. test1 with the argument opt opted-in? Or...
  2. test2 with the argument data passed and opt opted-out?

If it started to fail only after an update, that was probably a very welcome bug-fix.

If I remove the optional argument opt, then it compiles fine. If I add a data argument to test1 that has a different rank to test2's data, then it compiles fine.

Makes sense. Without the optional argument, both functions are completely unambiguous by number of arguments. Changing the rank of the argument also cause differentiation.

If I add another non-optional argument to both functions, I get the same error message.

Same problem. If both functions had an extra non-optional argument with type-kind-rank conformance, how would you resolve the call? Suppose the new argument is data_extra:

print*, Test(param1, param2)
  1. test1 with the argument data_extra passed and opt opted-in? Or...
  2. test2 with the arguments data and data_extra passed and opt opted-out?
like image 113
Rodrigo Rodrigues Avatar answered Nov 21 '25 01:11

Rodrigo Rodrigues



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!