-
Notifications
You must be signed in to change notification settings - Fork 185
Safety functions for working with allocatables #317
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
Comments
I think this would be useful. Note that the function can only be |
The current implementation of I see some similarity with the I can imagine |
A related concept using module option_mod
implicit none
type :: option_t
integer, allocatable :: some
end type
contains
function positive_int(value) result(res)
integer, intent(in) :: value
type(option_t) :: res
if (value > 0) then
allocate(res%some)
res%some = value
end if
end function
logical function is_some(opt)
type(option_t), intent(in) :: opt
if (allocated(opt%some)) then
is_some = .true.
else
is_some = .false.
end if
end function
logical function is_none(opt)
type(option_t), intent(in) :: opt
is_none = .not. is_some(opt)
end function
integer function some(opt)
type(option_t), intent(in) :: opt
some = opt%some
end function
end module
program main
use option_mod
implicit none
associate(res1 => positive_int(4), res2 => positive_int(-3))
call process(res1)
call process(res2)
end associate
contains
subroutine process(res)
type(option_t), intent(in) :: res
if (is_some(res)) then
associate(x => some(res))
print *, "Result: ", x
end associate
else
print *, "Value was not positive"
end if
end subroutine
end program With
With
and the program crashes due to a segmentation fault. Perhaps this approach makes sense, when there is no "default" value to be used -> None. |
Apologies in advance as this topic is still a bit fuzzy on my side.
The general idea is to provide some kind of safety functions to work with
allocatable
variables, the most commonly used is the deferred length character variable. There is one minor annoyance, an empty deferred length character can either be an empty string or unallocated, but usually we would like it to behave consistently for this case, i.e.len(dlc)
returning zero instead of a segmentation fault when thedlc
variable is not allocated.Adding the required additional checks is usually quite redundant, therefore providing a good and general mechanism to deal with such situations might be in scope for stdlib.
For the particular case of a deferred length character the function might look like (note that this function will create a copy of the character variable in the progress of making it safe):
Similar functions could be defined for
allocatable
integers, reals and logicals if we can agree on a “neutral” element for those data types.The naming is loosely oriented at Rust's
maybe
,ok
anderr
functionality, emulating something like this in stdlib would be the ultimate goal of the proposal.The text was updated successfully, but these errors were encountered: