File: operator_overloading_24.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 (52 lines) | stat: -rw-r--r-- 1,500 bytes parent folder | download | duplicates (2)
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
module operator_overloading_24_mod
  implicit none

  type :: string_t
    character(len=:), allocatable :: s
  contains
    procedure, private :: string_t_eq_string_t
    procedure, private :: string_t_eq_character
    procedure, private, pass(rhs) :: character_eq_string_t
    generic :: operator(==) => string_t_eq_string_t, string_t_eq_character, character_eq_string_t
  end type

contains

  elemental logical function string_t_eq_string_t(lhs, rhs)
    class(string_t), intent(in) :: lhs, rhs
    string_t_eq_string_t = lhs%s == rhs%s
  end function

  elemental logical function string_t_eq_character(lhs, rhs)
    class(string_t), intent(in) :: lhs
    character(len=*), intent(in) :: rhs
    string_t_eq_character = lhs%s == rhs
  end function

  elemental logical function character_eq_string_t(lhs, rhs)
    character(len=*), intent(in) :: lhs
    class(string_t), intent(in) :: rhs
    character_eq_string_t = lhs == rhs%s
  end function

end module

program operator_overloading_24
  use operator_overloading_24_mod, only: string_t
  implicit none

  ! string_t == string_t
  if (.not. (string_t("abc") == string_t("abc"))) error stop
  if (string_t("abc") == string_t("xyz")) error stop

  ! string_t == character
  if (.not. (string_t("abc") == "abc")) error stop
  if (string_t("abc") == "xyz") error stop

  ! character == string_t (pass(rhs))
  if (.not. ("abc" == string_t("abc"))) error stop
  if ("abc" == string_t("xyz")) error stop

  print *, "All tests passed."

end program