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

Muhammad Talha

04/28/2022, 7:22 AM
Hope this is the right place to ask this, since this is like an Either type. I’m using the following sealed class to return success or error types (credit to https://phauer.com/2019/sealed-classes-exceptions-kotlin/). It’s working great. I can use the
Outcome.Success
class with a value. However is it possible to use it without a value? There are some cases where I don’t want to pass a value in. I’m not quite sure what the definition would be for me not to pass a value in while not making value optional. Thanks!
Copy code
sealed class Outcome<out T: Any> {
    class Success<out T: Any>(val value: T) : Outcome<T>()
    class Error(val msg: String, val exp: Exception? = null) : Outcome<Nothing>()
}
s

simon.vergauwen

04/28/2022, 7:29 AM
If you don't want to use a value then you should use
Unit
which means
Success
without a meaningful value. In functional programming
Either<Error, Unit>
is typically used to signal that something can either fail with
Error
or succeed with no meaning value.
You can also find all this in the https://arrow-kt.io/ library if you're not familiar with it.
Either
for example can be found in Arrow Core. https://arrow-kt.io/docs/core/ and it offers nice DSLs for not requiring
flatMap
&
map
etc.
m

Muhammad Talha

04/28/2022, 7:51 AM
Thanks! I’m new to Kotlin so I didn’t know about this library. I’ll check this out.
🙌 1
s

simon.vergauwen

04/28/2022, 7:57 AM
My pleasure! If you have any questions there is an #arrow channel that is quite active.
👍 1
8 Views