Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
Announcements
Welcome to the Intel Community. If you get an answer you like, please mark it as an Accepted Solution to help others. Thank you!

select type parameterized data type

Hafsia__Aouididi
Beginner
108 Views

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.

0 Kudos
1 Solution
FortranFan
Honored Contributor II
108 Views

@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

 

View solution in original post

1 Reply
FortranFan
Honored Contributor II
109 Views

@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

 

View solution in original post

Reply