-
Notifications
You must be signed in to change notification settings - Fork 191
Preliminary implementation of default values #73
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ec6d900
Preliminary implementation of default values
nshaffer abb4a3c
Merge branch 'master' into master
certik b74d1a8
Update src/stdlib_experimental_default.f90
nshaffer ebde8b1
Update src/stdlib_experimental_default.f90
nshaffer 486be15
implement int8
nshaffer f39c35e
rename default -> optval
nshaffer fefbaf4
add tests for optval
nshaffer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
module default_m | ||
!! | ||
!! Provides a generic function `default`, which can be used to | ||
!! conveniently implement fallback values for optional arguments | ||
!! to subprograms. If `x` is an `optional` parameter of a | ||
!! subprogram, then the expression `default(x, y)` inside that | ||
!! subprogram evaluates to `x` if it is present, otherwise `y`. | ||
!! | ||
!! It is an error to call `default` with a single actual argument. | ||
!! | ||
!! For additional clarity, `default` be called with keyword argument | ||
!! for the fallback value, e.g., `default(x, to=1.0)`. | ||
!! | ||
use iso_fortran_env, only: sp => real32, dp => real64, qp => real128, int16, int32, int64 | ||
nshaffer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
implicit none | ||
|
||
|
||
private | ||
public :: default | ||
|
||
|
||
interface default | ||
module procedure default_sp | ||
module procedure default_dp | ||
module procedure default_qp | ||
module procedure default_int16 | ||
nshaffer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
module procedure default_int32 | ||
module procedure default_int64 | ||
module procedure default_logical | ||
module procedure default_character | ||
! TODO: complex kinds | ||
! TODO: differentiate ascii & ucs char kinds | ||
end interface default | ||
nshaffer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
contains | ||
|
||
|
||
function default_sp(x, to) result(y) | ||
real(sp), intent(in), optional :: x | ||
real(sp), intent(in) :: to | ||
real(sp) :: y | ||
|
||
if (present(x)) then | ||
y = x | ||
else | ||
y = to | ||
end if | ||
end function default_sp | ||
|
||
|
||
function default_dp(x, to) result(y) | ||
real(dp), intent(in), optional :: x | ||
real(dp), intent(in) :: to | ||
real(dp) :: y | ||
|
||
if (present(x)) then | ||
y = x | ||
else | ||
y = to | ||
end if | ||
end function default_dp | ||
|
||
|
||
function default_qp(x, to) result(y) | ||
real(qp), intent(in), optional :: x | ||
real(qp), intent(in) :: to | ||
real(qp) :: y | ||
|
||
if (present(x)) then | ||
y = x | ||
else | ||
y = to | ||
end if | ||
end function default_qp | ||
|
||
|
||
function default_int16(x, to) result(y) | ||
integer(int16), intent(in), optional :: x | ||
integer(int16), intent(in) :: to | ||
integer(int16) :: y | ||
|
||
if (present(x)) then | ||
y = x | ||
else | ||
y = to | ||
end if | ||
end function default_int16 | ||
|
||
|
||
function default_int32(x, to) result(y) | ||
integer(int32), intent(in), optional :: x | ||
integer(int32), intent(in) :: to | ||
integer(int32) :: y | ||
|
||
if (present(x)) then | ||
y = x | ||
else | ||
y = to | ||
end if | ||
end function default_int32 | ||
|
||
|
||
function default_int64(x, to) result(y) | ||
integer(int64), intent(in), optional :: x | ||
integer(int64), intent(in) :: to | ||
integer(int64) :: y | ||
|
||
if (present(x)) then | ||
y = x | ||
else | ||
y = to | ||
end if | ||
end function default_int64 | ||
|
||
|
||
function default_logical(x, to) result(y) | ||
logical, intent(in), optional :: x | ||
logical, intent(in) :: to | ||
logical :: y | ||
|
||
if (present(x)) then | ||
y = x | ||
else | ||
y = to | ||
end if | ||
end function default_logical | ||
|
||
|
||
function default_character(x, to) result(y) | ||
character(len=*), intent(in), optional :: x | ||
character(len=*), intent(in) :: to | ||
character(len=:), allocatable :: y | ||
|
||
if (present(x)) then | ||
y = x | ||
else | ||
y = to | ||
end if | ||
end function default_character | ||
|
||
end module default_m | ||
nshaffer marked this conversation as resolved.
Show resolved
Hide resolved
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.