• intrinsic procedure in interface

    From Beliavsky@21:1/5 to All on Fri Feb 18 09:59:33 2022
    For the code

    module norm_mod
    implicit none
    private
    public :: l2_norm
    intrinsic :: norm2
    interface l2_norm
    procedure :: norm2, norm2_int
    end interface l2_norm
    contains
    function norm2_int(v) result(vnorm)
    integer, intent(in) :: v(:)
    real :: vnorm
    integer :: n
    n = size(v)
    vnorm = sqrt(real(sum(v**2)))
    end function norm2_int
    end module norm_mod
    !
    program test_norm
    use norm_mod, only: l2_norm
    implicit none
    integer, allocatable :: v(:)
    v = [10,20,30]
    print*,l2_norm(v)
    print*,norm2(real(v))
    print*,l2_norm(real(v))
    end program test_norm

    gfortran says

    interface_procedure.f90:26:7:

    26 | print*,l2_norm(real(v))
    | 1
    Error: There is no specific function for the generic 'l2_norm' at (1)

    I thought it would use the intrinsic norm2 in this case, since it is one of the procedures in the interface of l2_norm. If I comment out line 26 the program compiles and runs fine, so my version of gfortran does implement norm2.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From rbader@21:1/5 to Beliavsky on Fri Feb 18 12:13:59 2022
    Beliavsky schrieb am Freitag, 18. Februar 2022 um 18:59:37 UTC+1:
    For the code

    module norm_mod
    implicit none
    private
    public :: l2_norm
    intrinsic :: norm2
    interface l2_norm
    procedure :: norm2, norm2_int
    end interface l2_norm
    contains
    function norm2_int(v) result(vnorm)
    integer, intent(in) :: v(:)
    real :: vnorm
    integer :: n
    n = size(v)
    vnorm = sqrt(real(sum(v**2)))
    end function norm2_int
    end module norm_mod
    !
    program test_norm
    use norm_mod, only: l2_norm
    implicit none
    integer, allocatable :: v(:)
    v = [10,20,30]
    print*,l2_norm(v)
    print*,norm2(real(v))
    print*,l2_norm(real(v))
    end program test_norm

    gfortran says

    interface_procedure.f90:26:7:

    26 | print*,l2_norm(real(v))
    | 1
    Error: There is no specific function for the generic 'l2_norm' at (1)

    I thought it would use the intrinsic norm2 in this case, since it is one of the procedures in the interface of l2_norm. If I comment out line 26 the program compiles and runs fine, so my version of gfortran does implement norm2.

    Your code runs afoul of a F2018 constraint dealing with procedure names appearing in named interfaces:

    C1508 (R1507) A procedure-name shall denote a nonintrinsic procedure that has an explicit interface.

    Regards
    Reinhold

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Beliavsky@21:1/5 to rbader on Fri Feb 18 12:38:35 2022
    On Friday, February 18, 2022 at 3:14:03 PM UTC-5, rbader wrote:
    Beliavsky schrieb am Freitag, 18. Februar 2022 um 18:59:37 UTC+1:
    For the code

    module norm_mod
    implicit none
    private
    public :: l2_norm
    intrinsic :: norm2
    interface l2_norm
    procedure :: norm2, norm2_int
    end interface l2_norm
    contains
    function norm2_int(v) result(vnorm)
    integer, intent(in) :: v(:)
    real :: vnorm
    integer :: n
    n = size(v)
    vnorm = sqrt(real(sum(v**2)))
    end function norm2_int
    end module norm_mod
    !
    program test_norm
    use norm_mod, only: l2_norm
    implicit none
    integer, allocatable :: v(:)
    v = [10,20,30]
    print*,l2_norm(v)
    print*,norm2(real(v))
    print*,l2_norm(real(v))
    end program test_norm

    gfortran says

    interface_procedure.f90:26:7:

    26 | print*,l2_norm(real(v))
    | 1
    Error: There is no specific function for the generic 'l2_norm' at (1)

    I thought it would use the intrinsic norm2 in this case, since it is one of the procedures in the interface of l2_norm. If I comment out line 26 the program compiles and runs fine, so my version of gfortran does implement norm2.
    Your code runs afoul of a F2018 constraint dealing with procedure names appearing in named interfaces:

    C1508 (R1507) A procedure-name shall denote a nonintrinsic procedure that has an explicit interface.

    Regards
    Reinhold

    Thank you. Below is alternative that does compile and run.

    module norm_mod
    implicit none
    private
    public :: l2_norm
    intrinsic :: norm2
    interface l2_norm
    module procedure :: norm2_real, norm2_int
    end interface l2_norm
    contains
    function norm2_int(v) result(vnorm)
    integer, intent(in) :: v(:)
    real :: vnorm
    integer :: n
    n = size(v)
    vnorm = sqrt(real(sum(v**2)))
    end function norm2_int
    !
    function norm2_real(v) result(vnorm)
    real , intent(in) :: v(:)
    real :: vnorm
    vnorm = norm2(v)
    end function norm2_real
    end module norm_mod
    !
    program test_norm
    use norm_mod, only: l2_norm
    implicit none
    integer, allocatable :: v(:)
    v = [10,20,30]
    print*,l2_norm(v)
    print*,norm2(real(v))
    print*,l2_norm(real(v))
    end program test_norm
    ! output:
    ! 37.41657
    ! 37.41657
    ! 37.41657

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)