Fortran · 791 bytes Raw Blame History
1 ! SELECT TYPE construct with TYPE IS and CLASS IS guards.
2 ! Tests: exact type matching, parent type matching via inheritance.
3 !
4 ! CHECK: circle
5 ! CHECK: 5.0
6 ! CHECK: is a shape
7 program test_select_type
8 implicit none
9 type :: shape
10 real :: x, y
11 end type
12 type, extends(shape) :: circle
13 real :: radius
14 end type
15
16 type(circle) :: c
17 c%x = 1.0
18 c%y = 2.0
19 c%radius = 5.0
20
21 ! TYPE IS: exact type match.
22 select type (c)
23 type is (circle)
24 print *, 'circle'
25 print *, c%radius
26 type is (shape)
27 print *, 'shape'
28 end select
29
30 ! CLASS IS: matches type or any extension.
31 select type (c)
32 class is (shape)
33 print *, 'is a shape'
34 class default
35 print *, 'unknown'
36 end select
37 end program
38