<@U3DE1TXKP> That's something else, there you prom...
# announcements
k
@robstoll That's something else, there you promise you can return whatever the caller want you to return (as long as it extends
T
), which is very rarely true.
r
I not really getting what you write. IMO a user cannot return anything as long as it extends T but has to return something which extend U which in turn has to extend T
k
Consider the following code:
Copy code
class Test<T: Any> {
    fun <U: T> foo(): U = TODO()
}

interface Parent
interface A: Parent
interface B: Parent

fun main(args: Array<String>) {
    val test = Test<Parent>()
    val a = test.foo<A>()
    val b = test.foo<B>()
}
I can just demand whatever type I want from
foo
because it promises it can return whatever .
But in practice that is almost never possible, even more so because
foo
doesn't have any generics type information at runtime.
r
wow... I would have never expected that. What is the sense of using a type-parameter as upper bound if not the instantiated type parameter is taken into account?
k
It only comes up in utility casting methods, eg. in android
findViewById
returns whatever type you want by casting.
r
IMO that's a misconception in Kotlin, I'll file a bug 😄
k
Haha it really isn't, that's how it works in Java too and how the code is expected to behave.
r
seriously? what is wrong with those guys 😄
please don't say it's also like that in Scala
k
Its like that in every language with generics I'd think?
That's really what the code is saying: I return the type specified by the generic parameter.
r
man, it seems I should invent a new language 😄
I agree on that one, but the type parameter has an upper bound restriction
k
That doesn't change anything really, now it's just "I return the type specified by the parameter, and that parameter has to extend something".
r
well, isn't that a fundamental change? Taking your example I would have expected that I can only call foo with subtypes of Parent
oh man... I am such a bad reader
k
That is true?
A
and
B
are subtypes.
r
A and B are subtypes of parent
k
Haha okay.
r
phuuu... the faith in programmers is restored again 😄
Thanks for the nice talk anyway 🙂
k
No problem, I love torturing myself by thinking about generics simple smile