Lukasz Kalnik
11/03/2023, 2:44 PMleft()
function here must have R?
, although R
is already by definition nullable?
class Either<L, R> private constructor(
private val left: L,
private val right: R,
) {
companion object {
fun <L, R> left(left: L): Either<L, R?> {
return Either(left, null)
}
}
}
Joffrey
11/03/2023, 2:46 PMR
is by definition nullable? R
is only nullable if the caller of this function specifies it as such.
This signature forces Either.left<Int, String>(42)
to return an Either<Int, String?>
. Were it not for the so called "definitely nullable" R?
, it would be expected to return Either<Int, String>
which is not possible given the body of the function (so defining the function without ?
would not compile)Lukasz Kalnik
11/03/2023, 2:47 PMR : Any?
that it's guaranteed to be nullable.
But indeed it depends on the concrete instantiation of the class.
It can be nullable, but doesn't have to.Lukasz Kalnik
11/03/2023, 2:48 PMJoffrey
11/03/2023, 2:48 PMMarc
01/02/2024, 11:44 AMsealed interface Either<out L, out R> {
data class Left<L>(val value: L) : Either<L, Nothing>
data class Right<R>(val value: R) : Either<Nothing, R>
companion object {
fun <L, R> left(left: L): Either<L, R> {
return Left(left)
}
fun <L, R> right(right: R): Either<L, R> {
return Right(right)
}
}
}
And then type each of them in isolation so you don’t need to pass null for non used branchesMarc
01/02/2024, 11:46 AMEither<L, R?>
you could not make it work with other non nullable R
perhapsLukasz Kalnik
01/02/2024, 12:46 PM