Question

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

Was it helpful?

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

OTHER TIPS

Arguably a better approach is to pass the argument through the subroutine's argument list, in which case you can use any local name that you like:

subroutine test(a)
...
end subroutine

and then call

call subroutine test(a%b%c%d%e)

And, while I'm writing, when you rename an entity on a use statement the form is

local-name => use-name 

and not, as you have it,

use-name => local-name
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top