https://kotlinlang.org logo
#feed
Title
# feed
l

Lionel Briand

10/28/2023, 5:41 PM
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

Aleksey Tomin

10/28/2023, 5:44 PM
May be better to use
Result
?
j

Jacob Ras

10/28/2023, 5:49 PM
@Aleksey Tomin in that case there's already michaelbull/kotlin-result ;-)
1
l

Lionel Briand

10/28/2023, 5:50 PM
@Aleksey Tomin Result is restricting the "error" case to always be a Throwable. I don't think it fit well in some cases.
m

Mikhail

10/28/2023, 6:09 PM
I used in my project my own
OperationResult<T>
because I need to serialize/deserialize object and transfer it via network.
c

CLOVIS

10/28/2023, 7:24 PM
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

curioustechizen

10/29/2023, 4:36 AM
The libs linked above like kotlin-result and arrow also have binding comprehensions as well as coroutine specific features.
👍 2
m

Marko Novaković

10/30/2023, 11:29 AM
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

Lionel Briand

10/30/2023, 1:48 PM
@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