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 came across this by passing JavaFX properties ( SimpleIntegerProperty , SimpleStringProperty , ... and SimpleObjectProperty<T> ) to the class constructor argument () -> Property<T> , where passing ::SimpleIntegerProperty works without problems, while ::SimpleObjectProperty fails, as in the above code example.
Is it possible to improve the compiler here or allow passing type parameters for references to the constructor / function? Does it make sense to use constructor references for simple lambda expressions here? Will it compile differently?
johnp source share