Why not the TRY macro, that will first check if error is allocated, and if so, either panic, or return it. Perhaps TRY can just panic, and a new macro FTRY can return it (F stands for a function, and you can only call FTRY from a function that also returns a Result — one issue is that typically the calling function might return a Result of a different type — to make it work with a macro, we can create generic procedures to fill any result with an error using the same syntax that you can then call from a macro). And if there is no error, it just gives back the value as an expression. I would not even “allocate” the integer:
type :: result_integer
integer :: value
type(fatal_error), allocatable :: error
end type
I guess the TRY would not be a macro, but a generic function:
integer function try(fn_result) result(r)
type(result_integer), intent(in) :: fn_result
if (allocated(fn_result.error)) error stop "PANIC"
r = fn_result%value
end function
And you use it like:
integer :: i
i = try(some_function(5))
I think that will work.
But how would you do the FTRY macro or a function? It needs to return from a function on error, but return an expression otherwise. Not sure how to do that at the moment. How does this work in SerenityOS (1, 2)?