Hans Ellegard
10/08/2020, 2:03 PMfun foo(dummy: Int?) {}
fun foo(dummy: String?) {}
// This causes a compilation error.
// You get
// reified.kt: (8, 5): None of the following functions can be called with the arguments supplied:
// public fun foo(dummy: Int?): Unit defined in root package in file reified.kt
// public fun foo(dummy: String?): Unit defined in root package in file reified.kt
inline fun <reified T> bar() {
// As the function is inlined and the type parameter reified, I'm hoping for static dispatch,
// i.e. depending on T, one of the overloads for foo() should be chosen.
foo(null as? T)
}
// This function has no problems compiling, static dispatch works as expected.
fun baz() {
foo(null as Int?)
foo(null as String?)
}
Why do I get a compilation error from bar
calling foo
? I guess it has something to do with Kotlin instantiating the bar
function even though it's supposed to be inlined yet never called (with an actual type parameter), but I haven't found the reason yet (I read https://kotlinlang.org/spec/runtime-type-information.html#runtime-available-types but found no explanation).Nir
10/08/2020, 2:05 PMHans Ellegard
10/08/2020, 2:05 PMNir
10/08/2020, 2:05 PMHans Ellegard
10/08/2020, 2:05 PMNir
10/08/2020, 2:06 PMNir
10/08/2020, 2:06 PMT
whatever the constraint says you can doNir
10/08/2020, 2:06 PMHans Ellegard
10/08/2020, 2:06 PMT
will beNir
10/08/2020, 2:07 PMHans Ellegard
10/08/2020, 2:07 PMT
, T
has no constraints?Nir
10/08/2020, 2:07 PMNir
10/08/2020, 2:07 PMHans Ellegard
10/08/2020, 2:07 PMNir
10/08/2020, 2:07 PMNir
10/08/2020, 2:08 PMbar<Float>
Nir
10/08/2020, 2:08 PMHans Ellegard
10/08/2020, 2:08 PMNir
10/08/2020, 2:09 PMFloat
meets the constraints of bar (trivially, because there are no constraints)Nir
10/08/2020, 2:09 PMNir
10/08/2020, 2:10 PMNir
10/08/2020, 2:10 PMNir
10/08/2020, 2:10 PMHans Ellegard
10/08/2020, 2:11 PMNir
10/08/2020, 2:11 PMNir
10/08/2020, 2:11 PMHans Ellegard
10/08/2020, 2:11 PMNir
10/08/2020, 2:12 PMNir
10/08/2020, 2:12 PMHans Ellegard
10/08/2020, 2:12 PMNir
10/08/2020, 2:13 PMNir
10/08/2020, 2:13 PMHans Ellegard
10/08/2020, 2:13 PMNir
10/08/2020, 2:14 PMHans Ellegard
10/08/2020, 2:14 PMHans Ellegard
10/08/2020, 2:14 PMNir
10/08/2020, 2:14 PMHans Ellegard
10/08/2020, 2:15 PMNir
10/08/2020, 2:15 PMNir
10/08/2020, 2:15 PMNir
10/08/2020, 2:16 PMNir
10/08/2020, 2:16 PMHans Ellegard
10/08/2020, 2:16 PMNir
10/08/2020, 2:16 PMNir
10/08/2020, 2:18 PMfun <T : Comparable<T>> sort(list: List<T>) { ... }
Hans Ellegard
10/08/2020, 2:18 PMsince you can't add base classes/interfaces to classes that are not yoursi think there's an issue for adding external interfaces though...
Nir
10/08/2020, 2:18 PMHans Ellegard
10/08/2020, 2:18 PMNir
10/08/2020, 2:18 PMNir
10/08/2020, 2:18 PMHans Ellegard
10/08/2020, 2:18 PMNir
10/08/2020, 2:19 PMNir
10/08/2020, 2:19 PMHans Ellegard
10/08/2020, 2:20 PMNir
10/08/2020, 2:20 PMJson = Nothing | String | Float | List<Json> | Map<Json>
Nir
10/08/2020, 2:21 PMvariant
Nir
10/08/2020, 2:21 PMNir
10/08/2020, 2:21 PMString
, Float
, etc inherit from your sealed classNir
10/08/2020, 2:21 PMNir
10/08/2020, 2:21 PMHans Ellegard
10/08/2020, 2:22 PMHans Ellegard
10/08/2020, 2:23 PMNir
10/08/2020, 2:24 PM