Sam Garfinkel
02/12/2020, 7:09 PMextract
here?
object Container {
val contained = mutableMapOf<String, Any>()
inline fun <reified T> extract(name: String): T = contained[name] as T
}
class Foo {
val bar: String by lazy {
Container.extract("Bar")
}
}
Because lazy
is declared to return a String
, shouldn’t the inferred type T
also be String
?Fleshgrinder
02/12/2020, 8:12 PM-Xnew-inference
(your example works with it).Zach Klippenstein (he/him) [MOD]
02/12/2020, 9:07 PMFleshgrinder
02/12/2020, 9:19 PMZach Klippenstein (he/him) [MOD]
02/12/2020, 9:23 PMZach Klippenstein (he/him) [MOD]
02/12/2020, 9:23 PMSam Garfinkel
02/12/2020, 9:56 PMWhy does type inference fail forhere?extract
Zach Klippenstein (he/him) [MOD]
02/12/2020, 9:59 PMlazy<String>
? Sorry if this is obvious, maybe I need another coffee…Sam Garfinkel
02/12/2020, 10:00 PMreified T
of extract
should be inferred from the return value of lazy
but isn’t with the current implementation of type inferrence.Fleshgrinder
02/12/2020, 10:10 PMlazy
writing Container.extract<String>("Bar")
is where it fails.Sam Garfinkel
02/12/2020, 10:14 PMContainer.extract<String>("Bar")
but my point was Container.extract("Bar)"
should be valid because generic type should match the return value of lazy
Fleshgrinder
02/12/2020, 10:42 PM