- Als neu kennzeichnen
- Lesezeichen
- Abonnieren
- Stummschalten
- RSS-Feed abonnieren
- Kennzeichnen
- Anstößigen Inhalt melden
Am trying to use select type and i have a parameterized data type.
I can't find whether it's possible to use the select type in order to do an upcasting.
And when i search in the standard i only find select type for derived type without parameters.
Thank you.
- Als neu kennzeichnen
- Lesezeichen
- Abonnieren
- Stummschalten
- RSS-Feed abonnieren
- Kennzeichnen
- Anstößigen Inhalt melden
@Hafsia, Aouididi,
With KIND type parameters you need to be explicit in terms of the kind and end up with TYPE IS (or CLASS IS) branches for each supported one; with LEN type parameters of parameterized types (PDTs) you can adopt the assumed length (=*) syntax just as with types of CHARACTER intrinsic or have branches for sets of supported specific lengths only.
A quick example:
module a
type :: a_t(X)
integer, kind :: X = 0
end type
end module
module e
use a, only : a_t
type, extends(a_t) :: e_t(Y)
integer, len :: Y = 0
end type
end module
use a, only : a_t
use e, only : e_t
blk1: block
class(a_t), allocatable :: foo
allocate ( a_t :: foo )
print *, "Block 1"
select type ( foo )
type is ( a_t(X=0) )
print *, "foo is of type a_t"
class default
end select
end block blk1
blk2: block
class(a_t), allocatable :: foo
allocate ( e_t :: foo )
print *, "Block 2"
select type ( foo )
type is ( e_t(X=0,Y=*) )
print *, "foo is of type e_t"
class default
end select
end block blk2
end
Upon execution, you should get:
Block 1 foo is of type a_t Block 2 foo is of type e_t
Link kopiert
- Als neu kennzeichnen
- Lesezeichen
- Abonnieren
- Stummschalten
- RSS-Feed abonnieren
- Kennzeichnen
- Anstößigen Inhalt melden
@Hafsia, Aouididi,
With KIND type parameters you need to be explicit in terms of the kind and end up with TYPE IS (or CLASS IS) branches for each supported one; with LEN type parameters of parameterized types (PDTs) you can adopt the assumed length (=*) syntax just as with types of CHARACTER intrinsic or have branches for sets of supported specific lengths only.
A quick example:
module a
type :: a_t(X)
integer, kind :: X = 0
end type
end module
module e
use a, only : a_t
type, extends(a_t) :: e_t(Y)
integer, len :: Y = 0
end type
end module
use a, only : a_t
use e, only : e_t
blk1: block
class(a_t), allocatable :: foo
allocate ( a_t :: foo )
print *, "Block 1"
select type ( foo )
type is ( a_t(X=0) )
print *, "foo is of type a_t"
class default
end select
end block blk1
blk2: block
class(a_t), allocatable :: foo
allocate ( e_t :: foo )
print *, "Block 2"
select type ( foo )
type is ( e_t(X=0,Y=*) )
print *, "foo is of type e_t"
class default
end select
end block blk2
end
Upon execution, you should get:
Block 1 foo is of type a_t Block 2 foo is of type e_t
- RSS-Feed abonnieren
- Thema als neu kennzeichnen
- Thema als gelesen kennzeichnen
- Diesen Thema für aktuellen Benutzer floaten
- Lesezeichen
- Abonnieren
- Drucker-Anzeigeseite