modulefortranderived-types

Rename localy a Fortran derived type loaded from a module


I'd like to improve the readability of my Fortran codes by using OOP. I end up with quite complex derived types, for example: a%b%c%d%e, defined in module mymod.

Now, in a subroutine I do

subroutine test
    use mymod, only: a
    ! lots of unrelevant stuff, especially with a%b%c%d%e.
end subroutine

My problem is the following: I would like to rename, in this particular subroutine, a%b%c%d%e to a simple newname.

I tested, for example: use mymod, only: a%b%c%d%e => newname but I a get compilation error that states:

Error: Syntax error in USE statement


Solution

  • You can use the associate construct to do this.

    subroutine test
        use :: mymod, only: a
        associate(newname => a%b%c%d%e)
            ! lots of unrelevant stuff, now using newname.
        end associate
    end subroutine