Ifvwm
06/30/2020, 1:31 AMandylamax
06/30/2020, 1:54 AMfun Foo(i:Int) : Bar { . . .}
fun Foo(s:String) : Bar { . . .}
If you are designing an api that should return an Int
or String
you have to do some extra work, i.e.
class Response<L,R>(val left:L,val right:R)
fun Foo(b:Bar):Response<Int,String> { . . .}
Michael de Kaste
06/30/2020, 11:07 AMsealed class Either<out A, out B> {
class Left<A>(val value: A): Either<A, Nothing>()
class Right<B>(val value: B): Either<Nothing, B>()
}
so that you can do this (with an Either<String, Int> example)
when(either){
is Left -> doWithString(either.value)
is Right -> doWithInt(either.value)
}