<@U10EJRH2L> Can you construct that type artificia...
# announcements
j
@Ruckus Can you construct that type artificially so the compiler is happy?
r
What do you mean?
j
Is there some way to call
validateAndParse
without casting
fromJson
to one specific type?
Ah, thanks,
intersection
type was what I needed. https://stackoverflow.com/a/44453604/3708426
@Ruckus Apparently JB thinks this is a bug: https://youtrack.jetbrains.com/issue/KT-7304
r
For intersections, yes. Not for unions.
j
What's the difference?
r
Union (commonly represented as
A | B
) means it can be either
A
or
B
. Intersections (commonly represented as
A & B
) means it has to be both
A
and
B
.
j
Ah, right.
r
For example (pseudocode):
Copy code
fun union(x: Int | String) { ... }
fun intersect(x: Int & String) { ... }

union(5)  // OK
union("fred")  // OK
intersect(5)  // ERR: 5 !is String
intersect("fred")  // ERR: "fred" !is Int
j
Right, and Kotlin doesn't support union types. Only intersection types.
Okay, that makes more sense.
r
Correct
j
Thanks!
👍 1
r
Usually people get around it by creating some sort of wrapper
Either<A, B>
class (a common idiom in functional programming).
j
Yep, big fan of
Either
types.
r
They can indeed be very nice depending on your style of programming. They can also be a pain if you try to force them into the wrong style.
If they work for you though, go for it.