sorry to not ask it during the meetting, but how u...
# arrow
t
sorry to not ask it during the meetting, but how union types could interact with when expression? I asume that a fold() method will be available, but most kotlin devs are familiar with exaustive when expression, could compiler plugin support it?
i
Could you rephrase your question a bit. I don’t understand the question. There is going to be an ide piece for
Meta
, which can give you an automatic exhaustive when expression on a Union Type. It’s not implemented, yet.
t
sorry, my question is, Are Union Types compatibles with exaustive when expression in the same way as sealed classes do?
i
Of course, because their expressed as inline Classes. Loosely speaking, because you have all possible ‘cases’ of Types in a Union, deriving a
when
should be pretty easy. So Something like this:
Copy code
val a: Union3<Int, String, Long> = TODO()

when(a){
  is Int -> doOnInt(a)
  is String -> doOnString(a)
  is Long -> doOnLong(a)
}
is possible. But don’t take that as is. This is just to showcase, how it may look like.
t
sounds great, thanks @Imran/Malic
i
In fact a
when
is not even needed, because
Unions
can do the conversion to Null, whenever the Type you care about is not the one you expect
r
when is needed if you don't go to null but the snippet above is how you would destructure a union
i
Your welcome @thanerian