How to pass a type parameter to a generic class constructor reference?

时光总嘲笑我的痴心妄想 提交于 2019-12-06 03:45:48

问题


Assume the following code:

class ConstructMe<T> {}
data class Test<T> constructor(var supplier: () -> ConstructMe<T>) {}

fun main(args: Array<String>) {
    works<Int>()
    breaks<Int>()
}

fun <T> works() {
    Test<T>({ ConstructMe<T>() }) // (1) any one class type parameter can be removed like:
    Test({ ConstructMe<T>() })  // (2) still works (class type inferred by argument type)
    Test<T>({ ConstructMe() })  // (3) still works (argument type inferred by class type)
}

fun <T> breaks() {
    Test<T>(::ConstructMe) // type interference failed (should probably work like (3); compiler improvement possible?)
    Test<T>(::ConstructMe<T>) // type interference failed & type argument not allowed (language change necessary?)
}

I've run into this by passing JavaFX properties (SimpleIntegerProperty, SimpleStringProperty, ... and SimpleObjectProperty<T>) to a generic class constructors () -> Property<T> argument, where passing ::SimpleIntegerProperty works without a problem, while ::SimpleObjectProperty fails like the above example code.

Is it possible to improve the compiler here or to allow passing type parameters to constructor/function references? Does it even make sense to use constructor references over simple lambda expressions here? Does it compile any differently?


回答1:


Yes, it is possible to improve the compiler here. It could infer type parameter for ConstructMe. See issue https://youtrack.jetbrains.com/issue/KT-10711.

For non-inline ounter function(in this case it is constructor of Test) there is no difference between lambda and callable reference to constructor. For both cases compiler creates anonymous class which has method invoke that creates an instance of ConstructMe.

But callable reference is more convenient than lambda in cases where constructor has a lot of parameters.



来源:https://stackoverflow.com/questions/37629644/how-to-pass-a-type-parameter-to-a-generic-class-constructor-reference

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!