Skip to content

Commit 89a8c79

Browse files
committed
Result Integer 0D
1 parent 844d29e commit 89a8c79

2 files changed

Lines changed: 23 additions & 92 deletions

File tree

src/example_fgen_basic/result/result.f90

Lines changed: 9 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ module m_result
1414
!!
1515
!! Holds either the result or an error.
1616

17-
class(*), allocatable :: data_v(..)
18-
!! Data i.e. the result (if no error occurs)
19-
!!
17+
! class(*), allocatable :: data_v(..)
18+
! MZ: assumed rank can only be dummy argument NOT type/class argument
19+
! Data i.e. the result (if no error occurs)
20+
!
2021
! Assumed rank array
2122
! (https://fortran-lang.discourse.group/t/assumed-rank-arrays/1049)
2223
! Technically a Fortran 2018 feature,
@@ -36,7 +37,8 @@ module m_result
3637
! procedure, public:: build
3738
! TODO: Think about whether build should be on the abstract class
3839
! or just on each concrete implementation
39-
procedure, public:: finalise, is_error
40+
procedure, public:: is_error
41+
final, public:: finalise
4042

4143
end type Result
4244

@@ -47,79 +49,15 @@ module m_result
4749

4850
contains
4951

50-
! See above about whether we include this here or not
51-
! Build should return a Result with an error if we try to set/allocate both
52-
! data and error
53-
! subroutine build(self, code, message)
54-
! !! Build instance
55-
!
56-
! class(ErrorV), intent(inout) :: self
57-
! ! Hopefully can leave without docstring (like Python)
58-
!
59-
! integer, intent(in) :: code
60-
! !! Error code
61-
! !!
62-
! !! Use [TODO: figure out xref] `NO_ERROR_CODE` if there is no error
63-
!
64-
! character(len=*), optional, intent(in) :: message
65-
! !! Error message
66-
!
67-
! self % code = code
68-
! if (present(message)) then
69-
! self % message = message
70-
! end if
71-
!
72-
! end subroutine build
73-
74-
!subroutine constructor(self, code, message)
75-
! !! Build instance
76-
!
77-
! class(*), allocatable :: data_v(..)
78-
! class(ErrorV), intent(inout) :: self
79-
! ! Hopefully can leave without docstring (like Python)
80-
! integer, intent(in) :: code = NO_ERROR_CODE
81-
! !! Error code
82-
! !!
83-
! !! Use [TODO: figure out xref] `NO_ERROR_CODE` if there is no error
84-
! character(len=*), optional, intent(in) :: message = ""
85-
! !! Error message
86-
!
87-
! self % code = code
88-
! if (present(message)) then
89-
! self % message = message
90-
! end if
91-
!
92-
!end subroutine constructor
93-
94-
function finalise(self) result(res)
52+
subroutine finalise(self)
9553
!! Finalise the instance (i.e. free/deallocate)
9654

9755
class(Result), intent(inout) :: self
9856
! Hopefully can leave without docstring (like Python)
9957

100-
! type(ResultNone) :: res
58+
if (allocated(self % error_v)) deallocate(self % error_v)
10159

102-
res = Result()
103-
104-
if (allocated(self % data_v) .and. allocated(self % error_v)) then
105-
deallocate(self % data_v)
106-
deallocate(self % error_v)
107-
call res % build(message="Both data and error were allocated")
108-
109-
elseif (allocated(self % data_v)) then
110-
deallocate(self % data_v)
111-
! No error - no need to call res % build
112-
113-
elseif (allocated(self % error_v)) then
114-
deallocate(self % error_v)
115-
! No error - no need to call res % build
116-
117-
else
118-
call res % build(message="Neither data nor error was allocated")
119-
120-
end if
121-
122-
end function finalise
60+
end subroutine finalise
12361

12462
pure function is_error(self) result(is_err)
12563
!! Determine whether `self` contains an error or not

src/example_fgen_basic/result/result_int.f90

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ module m_result_int
1010
implicit none
1111
private
1212

13-
type, extends(Result), public :: ResultInteger
13+
type, extends(Result), public :: ResultInteger0D
1414
!! Result type that holds integer values
1515
!!
1616
!! Holds either an integer value or an error.
1717

1818
integer, allocatable :: data_v
1919
!! Data i.e. the result (if no error occurs)
2020

21-
class(ErrorV), allocatable :: error_v
21+
! class(ErrorV), allocatable :: error_v
2222
!! Error
2323

2424
contains
@@ -27,20 +27,21 @@ module m_result_int
2727

2828
procedure, public:: build
2929
! `finalise` and `is_error` come from abstract base class
30+
final :: finalise
3031

31-
end type ResultInteger
32+
end type ResultInteger0D
3233

33-
interface ResultInteger
34+
interface ResultInteger0D
3435
!! Constructor interface - see build (TODO: figure out cross-ref syntax) for details
3536
module procedure :: constructor
36-
end interface ResultInteger
37+
end interface ResultInteger0D
3738

3839
contains
3940

40-
function constructor(res, data_v, error_v) result(self)
41+
function constructor(data_v, error_v) result(self)
4142
!! Build instance
4243

43-
type(ResultInteger), intent(out) :: self
44+
type(ResultInteger0D), intent(inout) :: self
4445
! Hopefully can leave without docstring (like Python)
4546

4647
class(ErrorV), intent(in) :: error_v
@@ -49,40 +50,32 @@ function constructor(res, data_v, error_v) result(self)
4950
integer, optional, intent(in) :: data_v
5051
!! Data
5152

52-
self%error_v = ErrorV()
53-
54-
if (present(error_v)) self%error_v = error_v
55-
if (present(data_v)) self%data_v = data_v
53+
call self%build(data_v=data_v, error_v=error_v)
5654

5755
end function constructor
5856

59-
subroutine build(self, res, data_v, error_v)
57+
subroutine build(self, data_v, error_v)
6058
!! Build instance
6159

62-
type(ResultInteger), intent(inout) :: self
60+
type(ResultInteger0D), intent(inout) :: self
6361
! Hopefully can leave without docstring (like Python)
6462

65-
!type(ResultNone), intent(inout) :: res
66-
!! Result
67-
6863
integer, optional, intent(in) :: data_v
6964
!! Data
7065

7166
class(ErrorV), optional, intent(in) :: error_v
7267
!! Error message
7368

74-
res = Result()
75-
76-
if (present(data_v) and present(error_v)) then
77-
call res % build(message="Both data and error were provided")
69+
if (present(data_v) .and. present(error_v)) then
70+
call self % build(message="Both data and error were provided")
7871
elseif (present(data_v)) then
7972
allocate(self % data_v, source=data_v)
8073
! No error - no need to call res % build
8174
elseif (present(error_v)) then
8275
allocate(self % error_v, source=error_v)
8376
! No error - no need to call res % build
8477
else
85-
call res % build(message="Neither data nor error were provided")
78+
call self % build(message="Neither data nor error were provided")
8679
end if
8780

8881
end subroutine build

0 commit comments

Comments
 (0)