Raed Ghazal
05/26/2022, 3:38 PMMichaeljon Hayden
05/26/2022, 3:49 PMin
or out
with T
in abstract class ViewHolder<T>
?Raed Ghazal
05/26/2022, 3:51 PMephemient
05/26/2022, 3:54 PMabstract class ViewHolder<out T>
ephemient
05/26/2022, 3:57 PMinterface F<T>
, F<A>
and F<A.B>
have no relation
when interface F<out T>
, F<A.B> : F<A>
when interface F<in T>
, F<A> : F<A.B>
Raed Ghazal
05/26/2022, 3:57 PMin
Raed Ghazal
05/26/2022, 3:58 PMin
doesn’t workephemient
05/26/2022, 3:58 PMRaed Ghazal
05/26/2022, 3:59 PMMichaeljon Hayden
05/26/2022, 3:59 PMMichaeljon Hayden
05/26/2022, 3:59 PMfun foo(): ViewHolder<in A> {}
ephemient
05/26/2022, 4:00 PMval vh: ViewHolder<A> = foo()
vh.bind(something that is A but not A.B)
should work but if it requires a A.B
then it can'tRaed Ghazal
05/26/2022, 4:00 PMfoo()
might return different type of ViewHolders
, depending on some value, each one inherits ViewHolder
with a different generic type from A
Raed Ghazal
05/26/2022, 4:02 PMRaed Ghazal
05/26/2022, 4:02 PMRaed Ghazal
05/26/2022, 4:10 PMif(item is A.B) bind(item)
Michaeljon Hayden
05/26/2022, 4:22 PMfoo(): ViewHolder<in A>
not work?Raed Ghazal
05/26/2022, 4:24 PMRaed Ghazal
05/26/2022, 4:26 PMMichaeljon Hayden
05/26/2022, 4:31 PMsealed class A {
class B: A()
class C: A()
}
abstract class VH<T> {
abstract fun bind(t: T)
}
class ABViewHolder: VH<A.B>() {
override fun bind(t: B) {
}
}
class ACViewHolder: VH<A.C>() {
override fun bind(t: C) {
}
}
fun foo(f: A): VH<out A> {
return when(f) {
is B -> ABViewHolder()
is C-> ACViewHolder()
else -> {}
}
}
Michaeljon Hayden
05/26/2022, 4:32 PMRaed Ghazal
05/26/2022, 4:37 PMfoo().bind(param)
won’t know which parameter to pass and it won’t be safe as @ephemient saidMichaeljon Hayden
05/26/2022, 5:06 PMsealed class Alpha {
object B: Alpha()
object C: Alpha()
}
abstract class VH<A: Alpha> {
abstract val type: A
abstract fun bind()
}
class ABViewHolder(
override val type: B
): VH<Alpha.B>() {
override fun bind() {
TODO("Not yet implemented")
}
}
class ACViewHolder(
override val type: C
): VH<Alpha.C>() {
override fun bind() {
TODO("Not yet implemented")
}
}
fun foo(type: Alpha): VH<out Alpha> {
return when(type) {
is B -> ABViewHolder(type)
is C-> ACViewHolder(type)
}
}
Raed Ghazal
05/26/2022, 7:11 PM