fortranderived-types

using fortran derived type variable without %


I define a type named MATRIX and a variable of this type named A as the following

TYPE MATRIX
    REAL, ALLOCATABLE, DIMENSION(:,:) :: MAT
END TYPE

TYPE(MATRIX) :: A

the usual way of constructing A and then using it is

ALLOCATE(A%MAT(2,2))

A%MAT(1,:) = [1,2]
A%MAT(2,:) = [3,4]

PRINT*, A%MAT

I'd like to know that if it's possible to work with variable A without having to write A%MAT. In other words, is there any workaround to rewrite the former block of code in the following form (using A instead of A%MAT)

ALLOCATE(A(2,2))

A(1,:) = [1,2]
A(2,:) = [3,4]

PRINT*, A

Solution

  • Unfortunately, the syntax a(1,:) = [1,2] where a is a derived type is not currently allowed by the Fortran standard (Fortran 2018 at the time of writing).

    There is a proposal to allow this in a future Fortran standard.