Quantcast
Channel: Intel® Fortran Compiler
Viewing all articles
Browse latest Browse all 3270

Getter functions for arrays

$
0
0

Hi,

I wonder if there exists an easy way to implement a getter function for an array valued attribute?

For instance, one has a derived item, for example the extensions of a process grid (pgrid). As this item can be retrieved lets say from an mpi subroutine using a given communcator, it would be cleaner not to store this informations redundantly:

...

type :: PGRID
    integer :: comm
  contains
    procedure :: psizes => get_psizes
end type PGRID

...

module function get_psizes(this) result(psizes)
    class(PGRID), intent(in) :: this
    integer :: psizes(2)
end function get_psizes

...

! now if one wants to access 'psizes' like an array,
! that does obviously produce an error because indexing
! is interpreted as  a second argument

dim1=pgrid%psizes(1)  ! -> error

 

I have found this more complicated workaraound, so that indexing should work, but I'm not very happy with it:

type :: PGRID
    integer :: comm
  contains
    procedure psizes => get_psizes
end type PGRID

...

interface get_psizes
    module function get_psizes_array(this,k) result(psizes)
        class(PGRID), intent(in) :: this
        integer, optional, intent(in) :: k(:)
        integer :: psizes(size(k))
    end function get_psizes_array

    module function get_psizes_single(this,k) result(psizes)
        class(PGRID), intent(in) :: this
        integer, optional, intent(in) :: k
        integer :: psizes
    end function get_psizes_single
end interface get_psizes

So is there any better solution for this problem?

- Thanks in advance


Viewing all articles
Browse latest Browse all 3270

Trending Articles