View previous topic :: View next topic |
Author |
Message |
simon
Joined: 05 Jul 2006 Posts: 299
|
Posted: Fri Aug 20, 2010 2:45 pm Post subject: Parameter derived types |
|
|
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?
Code: | MODULE m1
TYPE t1
CHARACTER(LEN=1) :: c1
CHARACTER(LEN=2) :: c2
END TYPE t1
TYPE(t1), PARAMETER :: a=(/'A','Aa'/)
END MODULE m1 |
|
|
Back to top |
|
 |
sparge
Joined: 11 Apr 2005 Posts: 371
|
Posted: Fri Aug 20, 2010 3:45 pm Post subject: |
|
|
If it's legal (I'm guessing not), logically you would write:
TYPE(t1), PARAMETER :: a=t1 ('A','Aa')
? |
|
Back to top |
|
 |
simon
Joined: 05 Jul 2006 Posts: 299
|
Posted: Fri Aug 20, 2010 4:45 pm Post subject: |
|
|
Thanks. I'll try.
A colleague thinks that it may be a Fortran 2003 feature. |
|
Back to top |
|
 |
sparge
Joined: 11 Apr 2005 Posts: 371
|
Posted: Fri Aug 20, 2010 5:25 pm Post subject: |
|
|
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
Code: | 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 |
|
|
Back to top |
|
 |
simon
Joined: 05 Jul 2006 Posts: 299
|
Posted: Fri Aug 20, 2010 8:16 pm Post subject: |
|
|
Great. That works. I needed an array so here's how that works:
Code: | 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 |
|
|
Back to top |
|
 |
|