<https://github.com/arrow-kt/arrow-meta/pull/64>
# arrow-meta
r
t
Will
Union<A,B>
be a subtype of
Union<B,A>
?
r
At the moment it isn’t. Do you have a use case in mind?
t
Well, I would expect this to be the case. Anything else would be counterintuitive and probably lead to a lot of pointless casts/conversions. What about
Union<A,B>
and
Union<A,B,C>
? Any function that accepts
Union<A,B,C>
should also accept
Union<A,B>
.
This should be fairly easy to do if you simply erase all unions to
Object
and check types in the kotlin compiler, but it would cost you type-safety on the Java side if that's something you care about.
r
This should be already working like that because:
Copy code
interface Union2<out A, out B>
interface Union3<out A, out B, out C> : Union2<A, B>
interface Union4<out A, out B, out C, out D> : Union3<A, B, C>
👌 1
And there is a single union synthetic impl not seen to the user that covers all arities
Copy code
inline class Union(val value: Any?) :
          Union2<Nothing, Nothing>,
          Union3<Nothing, Nothing, Nothing>,
          Union4<Nothing, Nothing, Nothing, Nothing> {
        
           inline operator fun <reified A> invoke(): A? =
             value as? A
           companion object {
             inline fun <reified A> union(union: Union): A? =
               value as? A
           }
        }