File: template_struct_01.f90

package info (click to toggle)
lfortran 0.45.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 46,332 kB
  • sloc: cpp: 137,068; f90: 51,260; python: 6,444; ansic: 4,277; yacc: 2,285; fortran: 806; sh: 524; makefile: 30; javascript: 15
file content (66 lines) | stat: -rw-r--r-- 1,406 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
module template_struct_01_m
  implicit none
  private
  public :: struct_t

  requirement r(t)
      type, deferred :: t
  end requirement

  template struct_t(t)
      require :: r(t)
      private
      public :: tuple

      type :: tuple
          type(t) :: fst
          type(t) :: snd
      end type

  contains

      function get_fst(p) result(r)
          type(tuple), intent(in) :: p
          type(t) :: r
          r = p%fst
      end function

      function get_snd(p) result(r)
        type(tuple), intent(in) :: p
        type(t) :: r
        r = p%snd
      end function

  end template

contains

  subroutine test_template()
      instantiate struct_t(integer), &
          only: int_tuple => tuple, get_int_fst => get_fst, &
                get_int_snd => get_snd
      instantiate struct_t(real), &
          only: real_tuple => tuple, get_real_fst => get_fst, &
                get_real_snd => get_snd
      type(int_tuple) :: ti
      ti%fst = 1
      print *, "First element: ", get_int_fst(ti)
      ti%snd = 2
      print *, "Second element: ", get_int_snd(ti)
      type(real_tuple) :: tr
      tr%fst = 1.
      print *, "First element: ", get_real_fst(tr)
      tr%snd = 2.
      print *, "Second element: ", get_real_snd(tr)
  end subroutine

end module

program template_struct_01
use template_struct_01_m

implicit none

call test_template()

end program template_struct_01