diesieben07
12/08/2020, 4:17 PMinterface Base<T>
and Concrete<T : Any> : Base<T>
. In a method which accepts Base<T>
I now need to check is Concrete<T>
, but that's impossible because in the method T
is not constrained to Any
(but it has to be, if I have a Concrete<T>
...
See thread for more code.diesieben07
12/08/2020, 4:17 PMinterface Base<T> {
fun foo(): T = TODO()
}
class Concrete<T : Any> : Base<T> {
fun otherFoo(): T = TODO()
}
fun <T> acceptBase(base: Base<T>): T = when (base) {
is Concrete<*> -> acceptConcrete(base) // <- this does not compile
else -> base.foo()
}
fun <T : Any> acceptConcrete(c: Concrete<T>) = c.otherFoo()
Chris Ruddell
12/08/2020, 5:27 PMfun <T: Any> acceptBase(base: Base<T>): T = when (base) {
is Concrete -> acceptConcrete(base)
else -> base.foo()
}
diesieben07
12/08/2020, 5:34 PMBase<Foo?>
Chris Ruddell
12/08/2020, 5:37 PMinterface Base<T> {
fun foo(): T = TODO()
}
class Concrete<T : Any?> : Base<T> {
fun otherFoo(): T = TODO()
}
fun <T: Any?> acceptBase(base: Base<T>): T = when (base) {
is Concrete -> acceptConcrete(base) // <- this does not compile
else -> base.foo()
}
fun <T : Any?> acceptConcrete(c: Concrete<T>) = c.otherFoo()
class Foo {
}
fun test() {
val base = acceptBase(Concrete<Foo?>())
}
diesieben07
12/08/2020, 5:38 PMConcrete<Foo?>
, which must be forbidden. Concrete
refines the constraints on Base
. Base
allows nulls, Concrete
does not.Scott Whitman
12/09/2020, 12:24 AMinterface Base<T: Any> {
fun foo(): T = TODO()
}
class Concrete<T : Any> : Base<T> {
fun otherFoo(): T = TODO()
}
inline fun <reified T: Any> acceptBase(base: Base<T>): T = when (base) {
is Concrete -> acceptConcrete(base) // <- this does not compile
else -> base.foo()
}
fun <T : Any> acceptConcrete(c: Concrete<T>): T = c.otherFoo()
diesieben07
12/10/2020, 10:00 PMacceptBase
with a Base<Foo?>
there.diesieben07
12/10/2020, 10:00 PMBase
to no longer accept nulls.