Skip to content

Commit ec6d900

Browse files
committed
Preliminary implementation of default values
1 parent 961b5ad commit ec6d900

File tree

3 files changed

+145
-0
lines changed

3 files changed

+145
-0
lines changed

src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ set(SRC
22
stdlib_experimental_ascii.f90
33
stdlib_experimental_io.f90
44
stdlib_experimental_error.f90
5+
stdlib_experimental_default.f90
56
)
67

78
add_library(fortran_stdlib ${SRC})

src/Makefile.manual

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
OBJS = stdlib_experimental_ascii.o \
22
stdlib_experimental_error.o \
33
stdlib_experimental_io.o \
4+
stdlib_experimental_default.o \
45

56
.PHONY: all clean
67
.SUFFIXES: .f90 .o
@@ -15,6 +16,7 @@ all: $(OBJS)
1516
stdlib_experimental_ascii.o: stdlib_experimental_ascii.f90
1617
stdlib_experimental_error.o: stdlib_experimental_error.f90
1718
stdlib_experimental_io.o: stdlib_experimental_io.f90
19+
stdlib_experimental_default.o: stdlib_experimental_default.f90
1820

1921
clean:
2022
$(RM) *.o *.mod

src/stdlib_experimental_default.f90

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
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

Comments
 (0)