Silverfrost Forums

Welcome to our forums

Parameter derived types

20 Aug 2010 1:45 #6804

I want to declare a derived type as a parameter, but cannot work out how to initialise it. Can anybody comment on the following, which generates a number of compilation errors? I can see that the second last line is trying to set up an array rather than the two components of the derived type, but what should the syntax be?

MODULE m1
  TYPE t1
     CHARACTER(LEN=1) :: c1
     CHARACTER(LEN=2) :: c2
  END TYPE t1
  TYPE(t1), PARAMETER :: a=(/'A','Aa'/)
END MODULE m1
20 Aug 2010 2:45 #6805

If it's legal (I'm guessing not), logically you would write:

TYPE(t1), PARAMETER :: a=t1 ('A','Aa')

?

20 Aug 2010 3:45 #6806

Thanks. I'll try. A colleague thinks that it may be a Fortran 2003 feature.

20 Aug 2010 4:25 #6807

I got curious and tried it too. The code below works for me. Whether it ought to, and whether more realistic code would, I have no idea 😃 but I have occasionally wanted to declare derived type parameters in the past, and been instinctively wary, so if it is legit I shall be incrementally happier as a result.

Andy

        program c
        use m1
        type (t1) b
        b = a
        stop
        end program c

      module m1 
        type t1 
           character(len=1) :: c1 
           character(len=2) :: c2 
        end type t1 
        type(t1), parameter :: a=t1 ('a','aa') 
      end module m1
20 Aug 2010 7:16 #6809

Great. That works. I needed an array so here's how that works:

MODULE m1
  TYPE t1
     CHARACTER(LEN=1) :: c1
     CHARACTER(LEN=2) :: c2
  END TYPE t1
  TYPE(t1), PARAMETER :: ab(2)=(/t1('A','Aa'),t1('B','Bb')/)
END MODULE m1
Please login to reply.