Hello, AndroidViewModel.getApplication() was acce...
# android
g
Hello, AndroidViewModel.getApplication() was accepted for boolean parameter, and i have only a runtime exception. Is that a problem with generic function? But it should extend a Application class
r
wouldn't that different types?
g
For me yes, but not for compiler :/
n
Are you sure, that it’s ok to call application inside of view model?
g
AndroidViewModel class holds this getApplication
n
Oh, sorry, I’ve missed that it is AndroidViewModel
s
As you suspect, this shouldn’t happen, as Boolean doesn’t extend Application. Is there another override of getMeasures maybe?
g
No, getMeasures is in a interface and only have one implementation
s
Huh, yeah you are right.. weird..
Distilled to remove any dependencies on Android/ViewModels:
Copy code
fun main() {
        val b: Boolean = num() // compiles, but shouldn't
        f(num()) // also compiles, but shouldn't
    }

    fun <T : Number> num(): T = 4 as T

    fun f(b: Boolean) = b
The Java equivalent works as expected
Copy code
void main() {
        final Integer n = num(); // Complies
        final Boolean b = num(); // Does not compile
        f(num()); // Does not compile
    }

    <T extends Number> T num() { return (T) new Integer(4); }

    void f(Boolean b) { }
I guess it’s a Kotlin bug
g
Yes, maybe a bug in the type inference?
If i move your num method to a class like
Copy code
class GenericClass<T : Number> {

    fun num() = 4 as T
}
i have to implicitly say the type
Copy code
val b: Boolean = GenericClass<Boolean>().num()
does not compile
s
I would be even more surprised if that also complied
g
I opened an issue (https://youtrack.jetbrains.com/issue/KT-43541), let's wait to know
👍 1
Thank you for your help