<@U7D34M1DX> why not use Left(....) and Right(...)...
# arrow
b
@Mike why not use Left(....) and Right(...) instead?
m
I'm not one of the contributors so don't know. Merely making a recommendation to ease usage. I agree that it is a bit clunky. And there's nothing preventing you from using your suggestion of explicitly constructing the object. I think that's what the byte code looks like as I think left/right are inline functions.
r
You can use both of them and some people prefer syntax and others constructor instantiation.
IDEA should have no problem suggesting always the right import provided you have typed your receiver value or it knows what the output type of the function is.
s
Sometimes it is more convenient to use something like
value?.left()
than
if (value == null) Left(value) else null
, for example.
m
When would you use the above? I thought the whole point of ‘Either’ was capturing presence/absence of the value. One shouldn’t return null for Either, should one?
s
An
Option
would be the presence/absence of a value. An
Either
has a different purpose (often carrying either a real value (right) or an error/exceptional value (left)).
m
Actually it's the same purpose in my mind. It's just the amount of information available about the absence. And in either case, one wouldn't set it to null. Unless your code snippet isn't complete enough? If the result of the 'if' is returned as an Either, then that feels like misuse to me. Returning a left or a null?
r
There is also
Option.toEither
which gives you back
Either<Unit, A>
. You can also adapt the absence to whatever other error type you need
Copy code
value: Int? = 0
object MissingValue
val result: Either<MissingValue, Int> = 
  Option.fromNullable(value).toEither().mapLeft { MissingValue }