https://kotlinlang.org logo
#functional
Title
# functional
j

João Gabriel Zó

02/16/2023, 5:23 PM
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

wakingrufus

02/16/2023, 8:57 PM
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

andylamax

02/17/2023, 7:28 AM
If it returns
nothing
, you can also go with
Result<Nothing?>
s

simon.vergauwen

02/17/2023, 8:45 AM
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.
5 Views