Blake Anderson
08/06/2023, 2:32 AMin
and out
bounds for a generic type? For example, the union of List<String>
and List<Object>
can be described by both in String
and out Object
, but it doesn't seem like there's any way to express this.Youssef Shoaib [MOD]
08/06/2023, 5:08 AMout
by default. Regardless, anything that's out Object
(or out Any?
in Kotlin) is just in whatever
because anything that's returned out
will be considered the most generic possible type Any?
Blake Anderson
08/06/2023, 6:03 AMclass Parent;
class Child;
class ReadWrite<T: Parent> {
fun read(): T
fun write(value: T)
}
I want to allow value
to be an instance of Child
- effectively, T: out Parent & in Child
. This doesn't seem possible, and I can't hack my way around this by defining fun write(value: Child)
because that causes an ambiguous overload for T = Child
.Sam
08/06/2023, 8:19 AMinterface Read<out T>
interface Write<in T>
interface ReadWrite<out R, in W: R>: Read<R>, Write<W>
Then, as you say, the union of ReadWrite<A, A>
and ReadWrite<B, B>
is ReadWrite<A, B>
😍. Very occasionally I've found myself wishing List
followed this pattern as well.Sam
08/06/2023, 8:20 AMMutableList<T, T>
everywhere instead of MutableList<T>
might be too high a price to pay 😄Blake Anderson
08/06/2023, 9:18 AM