Skip to content

Commit f23cf17

Browse files
committed
Result-type : 1D integer
1 parent 2f4ce95 commit f23cf17

2 files changed

Lines changed: 37 additions & 24 deletions

File tree

src/example_fgen_basic/result/result.f90

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ module m_result
3838
! TODO: Think about whether build should be on the abstract class
3939
! or just on each concrete implementation
4040
procedure, public:: is_error
41-
final, public:: finalise
41+
procedure, public :: clean_up
4242

4343
end type Result
4444

@@ -49,15 +49,15 @@ module m_result
4949

5050
contains
5151

52-
subroutine finalise(self)
52+
subroutine clean_up(self)
5353
!! Finalise the instance (i.e. free/deallocate)
5454

5555
class(Result), intent(inout) :: self
5656
! Hopefully can leave without docstring (like Python)
5757

58-
if (allocated(self % error_v)) deallocate(self % error_v)
58+
if (allocated(self % error_v)) deallocate(self%error_v)
5959

60-
end subroutine finalise
60+
end subroutine clean_up
6161

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

src/example_fgen_basic/result/result_int.f90

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

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

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

2121
! class(ErrorV), allocatable :: error_v
@@ -29,55 +29,68 @@ module m_result_int
2929
! `finalise` and `is_error` come from abstract base class
3030
final :: finalise
3131

32-
end type ResultInteger0D
32+
end type ResultInteger1D
3333

34-
interface ResultInteger0D
34+
interface ResultInteger1D
3535
!! Constructor interface - see build (TODO: figure out cross-ref syntax) for details
3636
module procedure :: constructor
37-
end interface ResultInteger0D
37+
end interface ResultInteger1D
3838

3939
contains
4040

4141
function constructor(data_v, error_v) result(self)
4242
!! Build instance
4343

44-
type(ResultInteger0D), intent(inout) :: self
44+
type(ResultInteger1D) :: self
4545
! Hopefully can leave without docstring (like Python)
4646

47-
class(ErrorV), intent(in) :: error_v
47+
class(ErrorV), intent(inout), optional :: error_v
4848
!! Error message
4949

50-
integer, optional, intent(in) :: data_v
50+
integer, allocatable, intent(in), optional :: data_v(:)
5151
!! Data
5252

53-
call self%build(data_v=data_v, error_v=error_v)
53+
call self%build(data_v_in=data_v, error_v_in=error_v)
5454

5555
end function constructor
5656

57-
subroutine build(self, data_v, error_v)
57+
subroutine build(self, data_v_in, error_v_in)
5858
!! Build instance
5959

60-
type(ResultInteger0D), intent(inout) :: self
60+
class(ResultInteger1D), intent(inout) :: self
6161
! Hopefully can leave without docstring (like Python)
6262

63-
integer, optional, intent(in) :: data_v
63+
integer, intent(in), optional :: data_v_in(:)
6464
!! Data
6565

66-
class(ErrorV), optional, intent(in) :: error_v
66+
class(ErrorV), intent(inout), optional :: error_v_in
6767
!! Error message
6868

69-
if (present(data_v) .and. present(error_v)) then
70-
call self % build(message="Both data and error were provided")
71-
elseif (present(data_v)) then
72-
allocate(self % data_v, source=data_v)
69+
if (present(data_v_in) .and. present(error_v_in)) then
70+
error_v_in%message="Both data and error were provided"
71+
elseif (present(data_v_in)) then
72+
allocate(self % data_v, source=data_v_in)
7373
! No error - no need to call res % build
74-
elseif (present(error_v)) then
75-
allocate(self % error_v, source=error_v)
74+
elseif (present(error_v_in)) then
75+
allocate(self % error_v, source=error_v_in)
7676
! No error - no need to call res % build
7777
else
78-
call self % build(message="Neither data nor error were provided")
78+
error_v_in%message="Neither data nor error were provided"
7979
end if
8080

8181
end subroutine build
8282

83+
subroutine finalise(self)
84+
!! Finalise instance
85+
86+
type(ResultInteger1D), intent(inout) :: self
87+
! Hopefully can leave without docstring (like Python)
88+
89+
if (allocated(self%data_v)) deallocate(self%data_v)
90+
if (allocated(self%error_v)) call self%clean_up()
91+
92+
end subroutine finalise
93+
94+
95+
8396
end module m_result_int

0 commit comments

Comments
 (0)