File: operator_overloading_15.f90

package info (click to toggle)
lfortran 0.59.0-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 56,736 kB
  • sloc: cpp: 168,052; f90: 74,272; python: 17,537; ansic: 7,705; yacc: 2,345; sh: 1,334; fortran: 895; makefile: 37; javascript: 15
file content (44 lines) | stat: -rw-r--r-- 1,137 bytes parent folder | download | duplicates (3)
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
module operator_overloading_15_mod
  type :: string_t
    character(len=:), allocatable :: str
  end type
  interface operator(==)
    module procedure string_array_equal
    module procedure string_equal
  end interface
contains
  logical function string_array_equal(a, b)
    type(string_t), intent(in) :: a(:), b(:)
    integer :: i
    string_array_equal = .true.
    do i = 1, size(a)
      if (a(i)%str /= b(i)%str) then
        string_array_equal = .false.
        return
      end if
    end do
  end function
  logical function string_equal(a, b)
    type(string_t), intent(in) :: a, b
    integer :: i
    string_equal = (a%str == b%str)
  end function
end module operator_overloading_15_mod

program operator_overloading_15
  use operator_overloading_15_mod
  type(string_t), allocatable, target :: a(:), b(:)
  type(string_t), pointer :: a2(:), b2(:)
  type(string_t), allocatable :: x, y
  allocate(a(1), b(1))
  allocate(x, y)
  a(1)%str = "Hello"
  b(1)%str = "HelloWorld"
  x%str = "Hello"
  y%str = "HelloWorld"
  a2 => a
  b2 => b
  if (a == b) error stop
  if (x == y) error stop
  if (a2 == b2) error stop
end program