Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
Announcements
FPGA community forums and blogs have moved to the Altera Community. Existing Intel Community members can sign in with their current credentials.
29329 Discussions

How to call base version of an overloaded type-bound procedure without "casting" the passed type to the base type

thomas_boehme
New Contributor II
620 Views
I would like to call a base version of an overridden type-bound procedure.

This can be done by using CALL ext%base%A(....)
However, this also "casts" (I don't know the proper word in FORTRAN2003) the passed argumentinto the base type.
This has the effect, that, if e.g. the subroutine A in the base calls another type-bound procedure, it actually calls the type-bound procedure of the base rather than the overrided version in extension.

Is there any possiblity to call a overriden subroutine of the base type, but provide the extended type as the passed argument?

See code below for an example. In this case, I would like to have ABase actually call BExtension rather than BBase.

regards,
Thomas
[fortran]    module types

    type base
    contains
        procedure A => ABase
        procedure B => BBase
    end type
    
    type, extends(base) :: extension
    contains
        procedure A => AExtension
        procedure B => BExtension
    end type    

    contains

        subroutine ABase(this)
        class (base) :: this
            write (*,*) 'Subroutine ABase has been called'
            CALL this%B()
        end subroutine

        subroutine BBase(this)
        class (base) :: this
            write (*,*) 'Subroutine BBase has been called'
        end subroutine

        subroutine AExtension(this)
        class (extension) :: this
            write (*,*) 'Subroutine AExtension has been called'
            CALL this%Base%A()
        end subroutine

        subroutine BExtension(this)
        class (extension) :: this
            write (*,*) 'Subroutine BExtension has been called'
        end subroutine

    end module

    program TypeBoundOverrides
    use types

    implicit none

    ! Variables
    type (Extension) :: ext
    ! Body of TypeBoundOverrides
    call ext%A()

    end program TypeBoundOverrides

[/fortran]

0 Kudos
2 Replies
Steven_L_Intel1
Employee
620 Views
I am not aware of a way you can do this. When you call this%Base%A, the passed argument has the type of Base. Perhaps you could do something with the fact that you can bind a single procedure to more than one type. So you might have a single AProc that serves both Base and Extension.
0 Kudos
thomas_boehme
New Contributor II
620 Views

Steve, thanks for your answer.

After posting, I had found a simple solution myself. As it is possible to bind the same procedures in a type using multiple names, I could just do the following:

[fortran]    type base
    contains
        procedure A => ABase
        procedure ABase => ABase
        procedure B => BBase
    end type
[/fortran]


Then, I can call ext%ABase() if A is overloaded, instead of calling ext%base%A()

The only thing that is not pefect is that you have to alter the base class to do this. However, that's not a real issue for our needs at the moment.

regards,
Thomas

0 Kudos
Reply