@@ -12,7 +12,7 @@ module m_result
1212 ! !
1313 ! ! Holds either the result or an error.
1414
15- class(* ), allocatable :: data (..)
15+ class(* ), allocatable :: data_v (..)
1616 ! ! Data i.e. the result (if no error occurs)
1717 ! !
1818 ! Assumed rank array
@@ -31,9 +31,10 @@ module m_result
3131
3232 private
3333
34- procedure , public :: build, finalise, is_error
34+ ! procedure, public:: build
3535 ! TODO: Think about whether build should be on the abstract class
3636 ! or just on each concrete implementation
37+ procedure , public :: finalise, is_error
3738
3839 end type Result
3940
@@ -68,16 +69,33 @@ module m_result
6869 !
6970 ! end subroutine build
7071
71- subroutine finalise (self )
72+ function finalise (self ) result(res )
7273 ! ! Finalise the instance (i.e. free/deallocate)
7374
7475 class(Result), intent (inout ) :: self
7576 ! Hopefully can leave without docstring (like Python)
7677
77- deallocate (self % data )
78- deallocate (self % error)
78+ type (ResultNone) :: res
7979
80- end subroutine finalise
80+ if (allocated (self % data_v) .and. allocated (self % error)) then
81+ deallocate (self % data_v)
82+ deallocate (self % error)
83+ call res % build(message= " Both data and error were allocated" )
84+
85+ elseif (allocated (self % data_v)) then
86+ deallocate (self % data_v)
87+ ! No error - no need to call res % build
88+
89+ elseif (allocated (self % error)) then
90+ deallocate (self % error)
91+ ! No error - no need to call res % build
92+
93+ else
94+ call res % build(message= " Neither data nor error was allocated" )
95+
96+ end if
97+
98+ end function finalise
8199
82100 pure function is_error (self ) result(is_err)
83101 ! ! Determine whether `self` contains an error or not
@@ -88,8 +106,7 @@ pure function is_error(self) result(is_err)
88106 logical :: is_err
89107 ! Whether `self` is an error or not
90108
91- is_err = self % error_v % is_error()
92- ! TODO: implement is_error on `error_v`
109+ is_err = allocated (self % error_v)
93110
94111 end function is_error
95112
0 commit comments