-
Notifications
You must be signed in to change notification settings - Fork 0
Result type 1D integer #33
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
base: cross-reference
Are you sure you want to change the base?
Changes from 9 commits
3c57801
9402f91
300694f
844d29e
89a8c79
2f4ce95
f23cf17
a5fba54
53b1910
02b5392
e2145a0
7937334
4b6f728
053fafd
61ba5dc
9dc7b3f
31ebe4c
0174330
f60b408
999f35f
3c2a6a4
4322463
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,75 @@ | ||||||||||||||||
| !> Result value | ||||||||||||||||
| !> | ||||||||||||||||
| !> Inspired by the excellent, MIT licensed | ||||||||||||||||
| !> https://github.com/samharrison7/fortran-error-handler | ||||||||||||||||
| module m_result | ||||||||||||||||
|
|
||||||||||||||||
| use m_error_v, only: ErrorV, NO_ERROR_CODE | ||||||||||||||||
|
|
||||||||||||||||
| implicit none (type, external) | ||||||||||||||||
| private | ||||||||||||||||
|
|
||||||||||||||||
| type, abstract, public :: Result | ||||||||||||||||
| !! Result type | ||||||||||||||||
| !! | ||||||||||||||||
| !! Holds either the result or an error. | ||||||||||||||||
|
|
||||||||||||||||
| ! class(*), allocatable :: data_v(..) | ||||||||||||||||
| ! MZ: assumed rank can only be dummy argument NOT type/class argument | ||||||||||||||||
| ! Data i.e. the result (if no error occurs) | ||||||||||||||||
| ! | ||||||||||||||||
| ! Assumed rank array | ||||||||||||||||
| ! (https://fortran-lang.discourse.group/t/assumed-rank-arrays/1049) | ||||||||||||||||
| ! Technically a Fortran 2018 feature, | ||||||||||||||||
| ! so maybe we need to update our file extensions. | ||||||||||||||||
| ! If we can't use this, just comment this out | ||||||||||||||||
| ! and leave each subclass of Result to set its data type | ||||||||||||||||
| ! (e.g. ResultInteger will have `integer :: data`, | ||||||||||||||||
| ! ResultDP1D will have `real(dp), dimension(:), allocatable :: data`) | ||||||||||||||||
|
|
||||||||||||||||
| class(ErrorV), allocatable :: error_v | ||||||||||||||||
| !! Error | ||||||||||||||||
|
|
||||||||||||||||
| contains | ||||||||||||||||
|
|
||||||||||||||||
| private | ||||||||||||||||
|
|
||||||||||||||||
| ! procedure, public:: build | ||||||||||||||||
| ! TODO: Think about whether build should be on the abstract class | ||||||||||||||||
| ! or just on each concrete implementation | ||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||
| procedure, public :: is_error | ||||||||||||||||
| procedure, public :: clean_up | ||||||||||||||||
|
|
||||||||||||||||
| end type Result | ||||||||||||||||
|
|
||||||||||||||||
| ! interface Result | ||||||||||||||||
| !! Constructor interface - see build (TODO: figure out cross-ref syntax) for details | ||||||||||||||||
| ! module procedure :: constructor | ||||||||||||||||
| ! end interface Result | ||||||||||||||||
|
|
||||||||||||||||
| contains | ||||||||||||||||
|
|
||||||||||||||||
| subroutine clean_up(self) | ||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No it is not. The automatic deallocation is done using the But for abstract types I've found a problem:
So The solution might be to have a
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. super makes sense to me |
||||||||||||||||
| !! Finalise the instance (i.e. free/deallocate) | ||||||||||||||||
|
|
||||||||||||||||
| class(Result), intent(inout) :: self | ||||||||||||||||
| ! Hopefully can leave without docstring (like Python) | ||||||||||||||||
|
|
||||||||||||||||
| if (allocated(self % error_v)) deallocate (self % error_v) | ||||||||||||||||
|
|
||||||||||||||||
| end subroutine clean_up | ||||||||||||||||
|
|
||||||||||||||||
| pure function is_error(self) result(is_err) | ||||||||||||||||
| !! Determine whether `self` contains an error or not | ||||||||||||||||
|
|
||||||||||||||||
| class(Result), intent(in) :: self | ||||||||||||||||
| ! Hopefully can leave without docstring (like Python) | ||||||||||||||||
|
|
||||||||||||||||
| logical :: is_err | ||||||||||||||||
| ! Whether `self` is an error or not | ||||||||||||||||
|
|
||||||||||||||||
| is_err = allocated(self % error_v) | ||||||||||||||||
|
|
||||||||||||||||
| end function is_error | ||||||||||||||||
|
|
||||||||||||||||
| end module m_result | ||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,94 @@ | ||||||||||||
| !> Result value for integers | ||||||||||||
| !> | ||||||||||||
| !> Inspired by the excellent, MIT licensed | ||||||||||||
| !> https://github.com/samharrison7/fortran-error-handler | ||||||||||||
| module m_result_int | ||||||||||||
|
|
||||||||||||
| use m_error_v, only: ErrorV | ||||||||||||
| use m_result, only: Result | ||||||||||||
|
|
||||||||||||
| implicit none (type, external) | ||||||||||||
| private | ||||||||||||
|
|
||||||||||||
| type, extends(Result), public :: ResultInteger1D | ||||||||||||
| !! Result type that holds integer values | ||||||||||||
| !! | ||||||||||||
| !! Holds either an integer value or an error. | ||||||||||||
|
|
||||||||||||
| integer, allocatable :: data_v(:) | ||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
To start, let's just do an int, rather than a 1D array of int
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I did an array because one has to take care of the allocation. But ok, no problem in doing scalars first.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. got it |
||||||||||||
| !! Data i.e. the result (if no error occurs) | ||||||||||||
|
|
||||||||||||
| ! class(ErrorV), allocatable :: error_v | ||||||||||||
| !! Error | ||||||||||||
|
znicholls marked this conversation as resolved.
Outdated
|
||||||||||||
|
|
||||||||||||
| contains | ||||||||||||
|
|
||||||||||||
| private | ||||||||||||
|
|
||||||||||||
| procedure, public :: build | ||||||||||||
| ! `finalise` and `is_error` come from abstract base class | ||||||||||||
| final :: finalise | ||||||||||||
|
|
||||||||||||
| end type ResultInteger1D | ||||||||||||
|
|
||||||||||||
| interface ResultInteger1D | ||||||||||||
| !! Constructor interface - see build (TODO: figure out cross-ref syntax) for details | ||||||||||||
| module procedure :: constructor | ||||||||||||
| end interface ResultInteger1D | ||||||||||||
|
|
||||||||||||
| contains | ||||||||||||
|
|
||||||||||||
| function constructor(data_v, error_v) result(self) | ||||||||||||
| !! Build instance | ||||||||||||
|
|
||||||||||||
| type(ResultInteger1D) :: self | ||||||||||||
| ! Hopefully can leave without docstring (like Python) | ||||||||||||
|
|
||||||||||||
| class(ErrorV), intent(inout), optional :: error_v | ||||||||||||
| !! Error message | ||||||||||||
|
|
||||||||||||
| integer, allocatable, intent(in), optional :: data_v(:) | ||||||||||||
| !! Data | ||||||||||||
|
|
||||||||||||
| call self % build(data_v_in=data_v, error_v_in=error_v) | ||||||||||||
|
|
||||||||||||
| end function constructor | ||||||||||||
|
|
||||||||||||
| subroutine build(self, data_v_in, error_v_in) | ||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
| !! Build instance | ||||||||||||
|
|
||||||||||||
| class(ResultInteger1D), intent(inout) :: self | ||||||||||||
| ! Hopefully can leave without docstring (like Python) | ||||||||||||
|
|
||||||||||||
| integer, intent(in), optional :: data_v_in(:) | ||||||||||||
| !! Data | ||||||||||||
|
|
||||||||||||
| class(ErrorV), intent(inout), optional :: error_v_in | ||||||||||||
| !! Error message | ||||||||||||
|
|
||||||||||||
| if (present(data_v_in) .and. present(error_v_in)) then | ||||||||||||
| error_v_in % message = "Both data and error were provided" | ||||||||||||
| else if (present(data_v_in)) then | ||||||||||||
| allocate (self % data_v, source=data_v_in) | ||||||||||||
| ! No error - no need to call res % build | ||||||||||||
| else if (present(error_v_in)) then | ||||||||||||
| allocate (self % error_v, source=error_v_in) | ||||||||||||
| ! No error - no need to call res % build | ||||||||||||
| else | ||||||||||||
| error_v_in % message = "Neither data nor error were provided" | ||||||||||||
| end if | ||||||||||||
|
|
||||||||||||
| end subroutine build | ||||||||||||
|
|
||||||||||||
| subroutine finalise(self) | ||||||||||||
| !! Finalise instance | ||||||||||||
|
|
||||||||||||
| type(ResultInteger1D), intent(inout) :: self | ||||||||||||
| ! Hopefully can leave without docstring (like Python) | ||||||||||||
|
|
||||||||||||
| if (allocated(self % data_v)) deallocate (self % data_v) | ||||||||||||
| if (allocated(self % error_v)) call self % clean_up() | ||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
|
|
||||||||||||
| end subroutine finalise | ||||||||||||
|
|
||||||||||||
| end module m_result_int | ||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.