samedi 4 juin 2016

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

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?





Aucun commentaire:

Enregistrer un commentaire