File: operator_overloading_28.f90

package info (click to toggle)
lfortran 0.61.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 61,892 kB
  • sloc: cpp: 181,767; f90: 92,175; python: 17,616; ansic: 10,170; yacc: 2,377; sh: 1,444; fortran: 892; makefile: 38; javascript: 15
file content (42 lines) | stat: -rw-r--r-- 975 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
module module_operator_overloading_28
  implicit none
  private
  public :: string_t

  type string_t
    character(len=:), allocatable :: s
  contains
    generic :: operator(==) => string_eq_string
    procedure, private :: string_eq_string
    procedure :: bracket
  end type

contains

  elemental function string_eq_string(lhs, rhs) result(res)
    class(string_t), intent(in) :: lhs
    type(string_t), intent(in) :: rhs
    logical :: res
    res = lhs%s == rhs%s
  end function

  elemental function bracket(self) result(res)
    class(string_t), intent(in) :: self
    type(string_t) :: res
    res%s = "[" // self%s // "]"
  end function

end module

program operator_overloading_28
  use module_operator_overloading_28, only : string_t
  implicit none

  type(string_t) :: arr(2), expected(2)
  arr(1)%s = "ab"
  arr(2)%s = "cd"
  expected(1)%s = "[ab]"
  expected(2)%s = "[cd]"
  if (.not. all(arr%bracket() == expected)) error stop
  print *, "PASS"
end program