There is no Either class in the standard library yet (it is likely to be added at some point). Meanwhile, you can define your own version of it along the lines of
abstract class Either<out A, out B> private () {
class Left<A>(val value: A): Either<A, Nothing>()
class Right<B>(val value: B): Either<Nothing, B>()
}
Unfortunately, our "when" expressions do not understand that is you check and Either for both Left and Right, it is exhaustive, yet, but this will be supported in the near future.
I notice there's no equivalent of Scala's "Try" in funKTionale -- is this a design choice (which I would understand -- I'm not convinced Try is the best solution) or have you simply not gotten around to implementing it?
Having this in standard API with features like default values, composition and transformations would be great.
Is it still considered to be added at some point? In 1.1 would be awesome
Like ‘?’ operator for nullable types (instead of a monadic Option type) a type Union operator could be a good fit for Kotlin too…
Some sample codes from the issue about how the API could look like.
Solution 1, with an Either type :
// return Int representation of s or an error message
fun toInt(s: String): Either<String, Int> = ...
toInt("1").map{it +1} // Right(2) or Success(2)
toInt("hello").getOrElse(-1) //-1
Solution 2 with a | operator :
// return Int representation of s or an error message
fun toInt(s: String): String|Int = ...
toInt("1")|.let{it +1} //Right(2) or Success(2)
toInt("hello")|:-1 //-1
Note : i’m not sure about ‘|:’ and ‘|?’ usage, maybe it would be more readable to use regular methods…