Is there a way to declare this Swift enum using se...
# getting-started
n
Is there a way to declare this Swift enum using sealed classes in Kotlin?
Copy code
enum Optional<T> {
    case None
    case Some(T)
}
d
Copy code
sealed class Optional<out T> {
    object None : Optional<Nothing>()
    data class Some(val value: T) : Optional<T>()
}
n
Thank you very much! I guess
Some
should be declared as
data class Some<out T>(val value: T): Optional<T>()
, otherwise it doesn’t compile?
o
Copy code
val object : MyClass? = ...
n
Olivier, my real-world enum looks like this now:
Copy code
sealed class MessageResult<out T> {
    data class ok<out T>(val value: T): MessageResult<T>()
    data class failed(val error: MessageError): MessageResult<Nothing>()
}
So it’s a little bit more complex than an optional. 🙂
I didn’t know about
Nothing
, but now I do, so the task is solved.
d
I guess
Some
should be declared as
data class Some<out T>(val value: T): Optional<T>()
, otherwise it doesn’t compile?
Yes, of course. Missed that, sorry.
o
n
optional and either are different imho
b
o
Actually Aleks needs Either, he have 2 possibilities : https://kotlinlang.slack.com/archives/C0B8MA7FA/p1544438340138100?thread_ts=1544437520.137300&amp;cid=C0B8MA7FA - ok with a value - error with a message This is what Either is for.
n
Incidentally, I was reading this article by @raulraja, which is in
Scala
, but the same functionalities are provided by
Arrow
. Differentiates when to use
Either
or
Try
or
Option
etc. https://www.47deg.com/presentations/2017/02/18/Functional-error-handling/