Skip to content

Commit bd38b94

Browse files
committed
added operators ==, /=
1 parent 40132dd commit bd38b94

File tree

3 files changed

+45
-4
lines changed

3 files changed

+45
-4
lines changed

doc/specs/stdlib_system.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ The following convenience type-bound procedures are provided:
3030
- `error()` returns a `logical` flag that is `.true.` in case of an error (`code /= 0`).
3131
- `handle([err])` assigns `err` to the calling variable or stops the program by calling `error stop`
3232

33+
### Overloaded Operators
34+
35+
operators `==`, `/=` are provided for comparing `type(fs_error)` variables and `integer` codes.
36+
3337
### Example
3438

3539
```

example/system/example_fs_error.f90

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
program example_fs_error
2-
use stdlib_system, only: fs_error
2+
use stdlib_system, only: fs_error, operator(/=)
33
implicit none
44

55
type(fs_error) :: err, err0
@@ -11,16 +11,16 @@ program example_fs_error
1111

1212
! Check success
1313
print *, 'Check error: ',err%error()
14-
print *, 'Check flag : ',err%code /= 0
14+
print *, 'Check flag : ',err /= 0
1515

1616
call err%handle(err0)
1717

18-
! Print flag
18+
! Print message
1919
print *, err0%print()
2020

2121
! Check success
2222
print *, 'Check error: ',err0%error()
23-
print *, 'Check flag : ',err0%code /= 0
23+
print *, 'Check flag : ',err0 /= 0
2424

2525
! call err%handle() ! stops the program
2626

src/stdlib_system.F90

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,19 @@ module stdlib_system
224224

225225
end type fs_error
226226

227+
interface operator(==)
228+
module procedure code_eq_err
229+
module procedure err_eq_code
230+
end interface operator(==)
231+
232+
interface operator(/=)
233+
module procedure code_neq_err
234+
module procedure err_neq_code
235+
end interface operator(/=)
236+
237+
public :: operator(==)
238+
public :: operator(/=)
239+
227240
interface runasync
228241
!! version: experimental
229242
!!
@@ -845,4 +858,28 @@ pure subroutine fs_error_handling(err,err_out)
845858
end if
846859
end subroutine fs_error_handling
847860

861+
pure logical function code_eq_err(code, err)
862+
integer, intent(in) :: code
863+
type(fs_error), intent(in) :: err
864+
code_eq_err = code == err%code
865+
end function code_eq_err
866+
867+
pure logical function err_eq_code(err, code)
868+
integer, intent(in):: code
869+
type(fs_error), intent(in) :: err
870+
err_eq_code = code == err%code
871+
end function err_eq_code
872+
873+
pure logical function code_neq_err(code, err)
874+
integer, intent(in) :: code
875+
type(fs_error), intent(in) :: err
876+
code_neq_err = code /= err%code
877+
end function code_neq_err
878+
879+
pure logical function err_neq_code(err, code)
880+
integer, intent(in) :: code
881+
type(fs_error), intent(in) :: err
882+
err_neq_code = code /= err%code
883+
end function err_neq_code
884+
848885
end module stdlib_system

0 commit comments

Comments
 (0)