I have a function which makes an external call and...
# functional
j
I have a function which makes an external call and returns nothing, but I need to do something based on if it succeeded or not. Is returning
Result<Unit>
the best thing to do here or is there anything else in the std library?
w
could do
Copy code
sealed class ExternalCallResult {
object Success : ExternalCallResult
object Failure : ExternalCallResult
}
if you dont need the value and exceptions parts of Result
a
If it returns
nothing
, you can also go with
Result<Nothing?>
s
I would personally go for
Result<Unit>
where
Unit
represents a side-effect without any meaningful result, so "succes" and the
Result.failure
holds the failure. The only value that can be given to
Nothing?
is
null
since
Nothing
has not value, so you can only fill in
null
. So it's isomorphic to
Unit
.
Arrow has
typealias Null = Nothing?
since
Nothing?
is the generic way to represent only
null
can be a valid value.