|
| 1 | +module default_m |
| 2 | + !! |
| 3 | + !! Provides a generic function `default`, which can be used to |
| 4 | + !! conveniently implement fallback values for optional arguments |
| 5 | + !! to subprograms. If `x` is an `optional` parameter of a |
| 6 | + !! subprogram, then the expression `default(x, y)` inside that |
| 7 | + !! subprogram evaluates to `x` if it is present, otherwise `y`. |
| 8 | + !! |
| 9 | + !! It is an error to call `default` with a single actual argument. |
| 10 | + !! |
| 11 | + !! For additional clarity, `default` be called with keyword argument |
| 12 | + !! for the fallback value, e.g., `default(x, to=1.0)`. |
| 13 | + !! |
| 14 | + use iso_fortran_env, only: sp => real32, dp => real64, qp => real128, int16, int32, int64 |
| 15 | + implicit none |
| 16 | + |
| 17 | + |
| 18 | + private |
| 19 | + public :: default |
| 20 | + |
| 21 | + |
| 22 | + interface default |
| 23 | + module procedure default_sp |
| 24 | + module procedure default_dp |
| 25 | + module procedure default_qp |
| 26 | + module procedure default_int16 |
| 27 | + module procedure default_int32 |
| 28 | + module procedure default_int64 |
| 29 | + module procedure default_logical |
| 30 | + module procedure default_character |
| 31 | + ! TODO: complex kinds |
| 32 | + ! TODO: differentiate ascii & ucs char kinds |
| 33 | + end interface default |
| 34 | + |
| 35 | + |
| 36 | +contains |
| 37 | + |
| 38 | + |
| 39 | + function default_sp(x, to) result(y) |
| 40 | + real(sp), intent(in), optional :: x |
| 41 | + real(sp), intent(in) :: to |
| 42 | + real(sp) :: y |
| 43 | + |
| 44 | + if (present(x)) then |
| 45 | + y = x |
| 46 | + else |
| 47 | + y = to |
| 48 | + end if |
| 49 | + end function default_sp |
| 50 | + |
| 51 | + |
| 52 | + function default_dp(x, to) result(y) |
| 53 | + real(dp), intent(in), optional :: x |
| 54 | + real(dp), intent(in) :: to |
| 55 | + real(dp) :: y |
| 56 | + |
| 57 | + if (present(x)) then |
| 58 | + y = x |
| 59 | + else |
| 60 | + y = to |
| 61 | + end if |
| 62 | + end function default_dp |
| 63 | + |
| 64 | + |
| 65 | + function default_qp(x, to) result(y) |
| 66 | + real(qp), intent(in), optional :: x |
| 67 | + real(qp), intent(in) :: to |
| 68 | + real(qp) :: y |
| 69 | + |
| 70 | + if (present(x)) then |
| 71 | + y = x |
| 72 | + else |
| 73 | + y = to |
| 74 | + end if |
| 75 | + end function default_qp |
| 76 | + |
| 77 | + |
| 78 | + function default_int16(x, to) result(y) |
| 79 | + integer(int16), intent(in), optional :: x |
| 80 | + integer(int16), intent(in) :: to |
| 81 | + integer(int16) :: y |
| 82 | + |
| 83 | + if (present(x)) then |
| 84 | + y = x |
| 85 | + else |
| 86 | + y = to |
| 87 | + end if |
| 88 | + end function default_int16 |
| 89 | + |
| 90 | + |
| 91 | + function default_int32(x, to) result(y) |
| 92 | + integer(int32), intent(in), optional :: x |
| 93 | + integer(int32), intent(in) :: to |
| 94 | + integer(int32) :: y |
| 95 | + |
| 96 | + if (present(x)) then |
| 97 | + y = x |
| 98 | + else |
| 99 | + y = to |
| 100 | + end if |
| 101 | + end function default_int32 |
| 102 | + |
| 103 | + |
| 104 | + function default_int64(x, to) result(y) |
| 105 | + integer(int64), intent(in), optional :: x |
| 106 | + integer(int64), intent(in) :: to |
| 107 | + integer(int64) :: y |
| 108 | + |
| 109 | + if (present(x)) then |
| 110 | + y = x |
| 111 | + else |
| 112 | + y = to |
| 113 | + end if |
| 114 | + end function default_int64 |
| 115 | + |
| 116 | + |
| 117 | + function default_logical(x, to) result(y) |
| 118 | + logical, intent(in), optional :: x |
| 119 | + logical, intent(in) :: to |
| 120 | + logical :: y |
| 121 | + |
| 122 | + if (present(x)) then |
| 123 | + y = x |
| 124 | + else |
| 125 | + y = to |
| 126 | + end if |
| 127 | + end function default_logical |
| 128 | + |
| 129 | + |
| 130 | + function default_character(x, to) result(y) |
| 131 | + character(len=*), intent(in), optional :: x |
| 132 | + character(len=*), intent(in) :: to |
| 133 | + character(len=:), allocatable :: y |
| 134 | + |
| 135 | + if (present(x)) then |
| 136 | + y = x |
| 137 | + else |
| 138 | + y = to |
| 139 | + end if |
| 140 | + end function default_character |
| 141 | + |
| 142 | +end module default_m |
0 commit comments