!     ******************************************************************
!     PARAMETERISERTE_DATATYPER.F90
!     Copyright(c) HiB 2000
!
!     Created: 03.09.2014 19:03:27
!     Author : SVEIN ATLE ENGESETH
!     Last change: SAE 29.12.2017 22.26.23
!     ******************************************************************

      MODULE PARAMETERISERTE_DATATYPER_MODULE
      !
      USE, INTRINSIC :: ISO_FORTRAN_ENV, ONLY : WP => REAL64, REAL32, REAL128
      !
      IMPLICIT NONE
      !
      PRIVATE
      !
      PUBLIC :: MATRISE, ABC
      !
      TYPE :: MATRISE(K, M, N)
        !
        INTEGER, KIND :: K = WP
        !
        INTEGER, LEN :: M = 3
        !
        INTEGER, LEN :: N = 4
        !
        REAL (K), DIMENSION(M, N) :: A
        !
      END TYPE MATRISE
      !
      CONTAINS
      !
      SUBROUTINE ABC(X)
        !
        IMPLICIT NONE
        !
        TYPE (MATRISE(WP, *, *)), INTENT(OUT) :: X
        !
        PRINT*, "abc:   ", X % K, X % M, X % N
        !
        ASSOCIATE(MAT => X % A(1:X % M,1:X % N), MM => X % M, NN => X % N)
          !
          PRINT*, "MM = ", MM, "   NN = ", NN
          !
          MAT = 13.0_WP
          !
          MAT(:,NN) = 33.0_WP
          !
          PRINT*, "MAT = ", MAT
          !
        END ASSOCIATE
        !
        ASSOCIATE(BBB => X % A, MM => X % M, NN => X % N) ! This makes BBB a zero sized matrix.
          !
          PRINT*, "MM = ", MM, "   NN = ", NN
          !
          BBB = 222.0_WP
          !
          BBB(:,NN) = -133.0_WP
          !
          PRINT*, "BBB = ", BBB
          !
        END ASSOCIATE
        !
      END SUBROUTINE ABC
      !
      END MODULE PARAMETERISERTE_DATATYPER_MODULE
      !
      PROGRAM PARAMETERISERTE_DATATYPER
      !
      USE, INTRINSIC :: ISO_FORTRAN_ENV, ONLY : WP => REAL64, REAL32, REAL128
      !
      USE, NON_INTRINSIC :: PARAMETERISERTE_DATATYPER_MODULE
      !
      IMPLICIT NONE
      !
      TYPE (MATRISE(REAL32, 3, 4)) :: A
      !
      TYPE (MATRISE(WP, 4, 1)) :: B
      !
      TYPE (MATRISE(K = REAL128, M = 5, N = 2)) :: C
      !
      TYPE (MATRISE(K = WP, M = 3, N = 2)) :: Y
      !
      TYPE (MATRISE(M = 13, N = 12)) :: D
      !
      A % A = 1.0_REAL32
      !
      PRINT*, "A:"
      !
      PRINT*, A
      !
      ASSOCIATE(MAT => B % A)  ! But here, it makes MAT the correct size.
        !
        MAT = 13.0_WP
        !
        PRINT*, "B     MAT = ", MAT
        !
      END ASSOCIATE
      !
      Y % A = - 3.0_WP
      !
      PRINT*, "Y:"
      !
      PRINT*, Y
      !
      CALL ABC(Y)
      !
      PRINT*, "Y:"
      !
      PRINT*, Y
      !
      END PROGRAM PARAMETERISERTE_DATATYPER
      !
