Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

PARAMETER of Defined Type

Svein-Atle_Engeseth
291 Views

Hi,
Please see the following program
 

      MODULE TEST_CONSTRUCTOR
      IMPLICIT NONE
      TYPE :: ABC
        PRIVATE
        INTEGER :: I = 0
        REAL :: R = 0.0
        CONTAINS
        PROCEDURE, PUBLIC :: PRINTME
      END TYPE ABC
      !
      INTERFACE ABC
        MODULE PROCEDURE INIT_ABC
      END INTERFACE
      !
      TYPE (ABC), PARAMETER :: P = ABC(3, 4.0)
      PRIVATE
      PUBLIC :: ABC, P
      !
      CONTAINS
      !
      FUNCTION INIT_ABC(I, R) RESULT(RESULTAT)
        IMPLICIT NONE
        INTEGER, INTENT(IN) :: I
        REAL, INTENT(IN) :: R
        TYPE (ABC) :: RESULTAT
        RESULTAT % I = I
        RESULTAT % R = R
      END FUNCTION INIT_ABC
      !
      SUBROUTINE PRINTME(X)
        IMPLICIT NONE
        CLASS(ABC), INTENT(IN) :: X
        WRITE(6,10) X % I, X % R
   10   FORMAT(/,1X,"I = ",I5,5X,"R = ",ES15.6)
      END SUBROUTINE PRINTME
      !
      END MODULE TEST_CONSTRUCTOR
      !
      PROGRAM TEST_CONSTRUCTOR_PGM
      USE, NON_INTRINSIC :: TEST_CONSTRUCTOR
      IMPLICIT NONE
  !    TYPE (ABC) :: A = ABC(3, 4.0) ! Uncomment this line and I get a compilation error when INTERFACE ABC is defined.
      !
      ! Does this mean that it is impossible to have a statement like that shown below if ABC has private components?
      !
  !    TYPE (ABC), PARAMETER :: Y = ABC(1, 1.0)
      !
      TYPE (ABC) :: A
      TYPE (ABC) :: B
      B = ABC(5, 12.0)
      CALL A % PRINTME()
      CALL B % PRINTME()
      CALL P % PRINTME()
      END PROGRAM TEST_CONSTRUCTOR_PGM

The line TYPE(ABC) :: A = ABC(3, 4.0) isn't allowed because of private components.
So what do I do if I want to have a PARAMETER of derived type defined in a program, assuming I don't have access to the module?

Regards,
Svein-Atle Engeseth

 

0 Kudos
3 Replies
jimdempseyatthecove
Honored Contributor III
291 Views

What is the purpose of "I want to have a PARAMETER of derived type defined in a program"?

A reason that I can think of is to effectively make the variable (Y) Read-Only. Is this what you intend?

If so, consider making an encapsulating type, containing a private instance of ABC (as Y), and supplying the appropriate GET functions.

FWIW this could be a case for the Fortran committee to consider an attribute READONLY.
While you can use INTENT(IN) on a dummy argument, you cannot on a module variable.

Jim Dempsey

0 Kudos
FortranFan
Honored Contributor II
291 Views

Svein-Atle Engeseth wrote:

.. The line TYPE(ABC) :: A = ABC(3, 4.0) isn't allowed because of private components. ..

The issue is not about private components of the derived type, especially because the code includes an INTERFACE with the same name as the derived type with a MODULE PROCEDURE to what is effecitvely a type constructor.  Rahter, it has to do with what the Fortran standard stipulates regarding the use of "constant expressions"  in a compile-time situation "to specify an initial value for an entity".  See this:

https://software.intel.com/en-us/fortran-compiler-18.0-developer-guide-and-reference-constant-expressions#8EE5286D-1A77-44E6-8AC8-BA11125DB053​

Svein-Atle Engeseth wrote:

.. So what do I do if I want to have a PARAMETER of derived type defined in a program, assuming I don't have access to the module?. ..

It is unclear what this means, what is the desired coding need, particularly considering the "don't have access to the module" aspect.

0 Kudos
Svein-Atle_Engeseth
291 Views

Hi Jim,
The thought was that an entity of a user defined type could have the attribute PARAMETER just like an entity of intrinsic type.

Fortranfan,
By not having access to the module I mean I am not allowed to change it's content.
So if I want to have a variable of type ABC with the PARAMETER attribute I will have to specify that in the program, not in the module.
And I want the type ABC to have private components.

Regards,
Svein-Atle

 

 

0 Kudos
Reply