https://kotlinlang.org logo
Title
i

Ifvwm

06/20/2019, 5:12 AM
how I can create a type, that tpe contain String and Int?
k

karelpeeters

06/20/2019, 5:26 AM
You can't directly, either use
Any
or something like
Either<Int, String>
from #arrow.
i

Ifvwm

06/20/2019, 5:28 AM
this arrow means?
Either Int String is what I want, but I don't know how to do Left 3 or Right "3" in kotlin
does kotlin have value constructor?
k

kartoffelsup

06/20/2019, 5:35 AM
A Pair<Int, String> with
42 to "string"
?
i

Ifvwm

06/20/2019, 5:41 AM
no
f :: Either Int String; f x = x; f (Left 3) will get Left 3, f (Right "S") will get Right "S"
the function f's parameter type can be Int or String wrapped by Either
it's about polymorphic, for example, f(x) if x is String, then make reverse x and return it, if x is Int, then plus one and return it
x's type can be Int or String
and we still can get 3 from (Left 3) by ff (Left x) = x
now how to do that in kotlin?
g

gildor

06/20/2019, 10:07 AM
You can just use Sealed classes, same way as it done in #arrow library
k

karelpeeters

06/20/2019, 5:49 PM
@Ifvwm #arrow is just a library that has a class that handles what you want to do.