Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29244 Discussions

Generic procedure reference has two or more specific procedure with the same type/rank/keyword signature

OP1
New Contributor III
457 Views

Hi,

I would like to know if it is possible to have a generic function returning values of different types, depending on the value it is assigned to.

For instance, the following program will trigger an error with IVF 10.1 . It's obvious I could transform these functions into subroutines (in which case it would work), but I thought the compiler would recognize the type of variable the function result is passed to in the main program.

Olivier

[fortran]PROGRAM MAIN
    USE MODULE_INTERFACES
    IMPLICIT NONE
    INTEGER I
    REAL(8) R
    I = GENERIC_FUNCTION(1)
    R = GENERIC_FUNCTION(1)
    WRITE(*,*) 'I = ',I
    WRITE(*,*) 'R = ',R
END PROGRAM MAIN

MODULE MODULE_INTERFACES
    IMPLICIT NONE
    INTERFACE GENERIC_FUNCTION
        INTEGER FUNCTION IFUN(I)
            INTEGER,INTENT(IN) :: I
        END FUNCTION IFUN
        REAL(8) FUNCTION RFUN(I)
            INTEGER,INTENT(IN) :: I
        END FUNCTION RFUN
    END INTERFACE GENERIC_FUNCTION
END MODULE MODULE_INTERFACES

INTEGER FUNCTION IFUN(I)
    IMPLICIT NONE
    INTEGER,INTENT(IN) :: I
    IFUN = I
END FUNCTION IFUN

REAL(8) FUNCTION RFUN(I)
    IMPLICIT NONE
    INTEGER,INTENT(IN) :: I
    RFUN = COS(DBLE(I))
END FUNCTION RFUN
[/fortran]

0 Kudos
3 Replies
TimP
Honored Contributor III
457 Views
The Fortran standard specifies that the selection is made by examining the argument list without reference to the data type to which a function result is assigned. The compiler would be perfectly happy to convert either type of function result to match the assignment, but is required by the standard to diagnose the duplicate generic function argument lists.
0 Kudos
IDZ_A_Intel
Employee
457 Views

Because functions are meant for use in expressions, things could get rather confusing if selection on the "use of the function result" was considered. Consider:

[fortran]real :: r(10)   ! different kind and rank
r = 2 + generic_funtion(1)  ! mixed types in expression
[/fortran]

[bash]write (*,*) generic_funtion(1)
[/bash]

[fortran]interface gen_sub
  module procedure gen_sub_i  ! takes an integer
  module procedure gen_sub_r  ! takes a real
end interface gen_sub

call gen_sub(generic_function(1)) 
[/fortran]

0 Kudos
OP1
New Contributor III
457 Views

Yes, that is true. Using a generic subroutine to achieve this makes a lot more sense. I have to say though that I have never really liked the mixed arguments feature in expressions (that and the fact that IMPLICIT NONE is optional...). I suppose these are legacy oddities which will persist in the Fortranstandard for decades to come :) ...

Thanks for your answers!

Olivier

0 Kudos
Reply