Hello ! Do you need a simple `Either` or `Option` ...
# feed
l
Hello ! Do you need a simple
Either
or
Option
class in your current project ? I found these patterns quite useful and I've used them in my code for quite a while. I have taken the time to share my implementation with everyone ! If you are interested, you can check out the published lib and source here: https://github.com/L-Briand/either This lib is multiplatform, feel free to test it on your project and share your thought. If you like it, maybe star the projet too. Thanks đź‘‹
a
May be better to use
Result
?
j
@Aleksey Tomin in that case there's already michaelbull/kotlin-result ;-)
âž• 1
l
@Aleksey Tomin Result is restricting the "error" case to always be a Throwable. I don't think it fit well in some cases.
m
I used in my project my own
OperationResult<T>
because I need to serialize/deserialize object and transfer it via network.
c
If your goal is to learn, go ahead; but if you're using this in a large project, I recommend using #arrow to avoid creating duplicate features.
âž• 7
👍 2
c
The libs linked above like kotlin-result and arrow also have binding comprehensions as well as coroutine specific features.
👍 2
m
does it offer something beyond what arrow does? I see it’s just either and option so you don’t import anything else with this, which is cool.
why option type tho? any advantages over using nullable types? nullable types already have language level support with simple sintax. and it’s easy to do monadic computation/composition over nullable types with
?.
and
:?
l
@Marko Novaković Depending on the case, a nullable variable can be a valid state. Wrapping it inside an Option can be useful to discriminate valid and invalid state. Here is a simple example of a timeout function returning an option as its result. The return type can be null and it should not be considered invalid:
Copy code
fun timeout(block: () -> T) = try { 
    val result = withTimeout(1.second) { block() }
    Some(result)
} catch (e: TimeoutCancellationException) {
    None
}
👌 1
@Marko Novaković It also helps to have this class on some situation regarding serialization / deserialization. https://github.com/L-Briand/either#option
🔥 1