String Bound Function Overloading in Fortran 2003

I have a Fortran T derived type that contains data arrays of (many) different ranks and types. These arrays are hidden inside a complex data structure, and I would like to have a getter function that does the following:

a => T%get(data_id)

where "a" is an array pointer of a given type, and data_id is an integer that is used to search for data within the data structure. I do this by overloading many of the get_thistype () functions under a common name.

TYPE T
   PROCEDURE :: get_real
   PROCEDURE :: get_integer
   GENERIC   :: get => get_real,get_integer
END TYPE

This works if the get_thistype () routines are routines, but not if they are written as functions. This means that my code is as follows:

CALL T%get(a,data_id)

. , , ? ?

+5
2

fortran (), , . , , LHS RHS. , .

+5

, :

, , . :

X.f90:

MODULE XModule

TYPE :: X
   INTEGER, DIMENSION(:), POINTER :: IntArray
   REAL,    DIMENSION(:), POINTER :: RealArray
END TYPE

INTERFACE ASSIGNMENT (=)
   MODULE PROCEDURE PointToInt
   MODULE PROCEDURE PointToReal
END INTERFACE

CONTAINS

SUBROUTINE PointToInt(Ip, V)
   INTEGER, POINTER, DIMENSION(:), INTENT(OUT) :: Ip
   TYPE(X), INTENT(IN) :: V
   Ip => V%IntArray
END SUBROUTINE PointToInt

SUBROUTINE PointToReal(Rp, V)
   REAL, POINTER, DIMENSION(:), INTENT(OUT) :: Rp
   TYPE(X), INTENT(IN) :: V
   Rp => V%RealArray
END SUBROUTINE PointToReal

END MODULE

Driver.f90:

PROGRAM Driver
USE XModule
TYPE(X) :: Var
INTEGER, DIMENSION(:), POINTER :: I
REAL, DIMENSION(:), POINTER :: R

ALLOCATE(Var%IntArray(2))
ALLOCATE(Var%RealArray(3))

Var%IntArray = [1, 2]
Var%RealArray = [1., 2., 3.]

I = Var
PRINT*, I

R = Var
PRINT*, R

END PROGRAM

:

           1           2
   1.000000       2.000000       3.000000    

, .

0

All Articles