lundi 9 novembre 2020

How to return a specific constructor when using reflection?

I'm reading the following document : Constructor References

There is explained how to send a referenced to constructor. But they don't described a situation when there is more than one constructor (overloaded constructor). The following code makes an error in compilation because of overload resolution ambiguity:

     open  class Animal (val stttt:String, val str:String = "hjm") {
        constructor(number1: Int, _number2: Int) :this("dsa")
        {
            println("runnnnnn")
        }
     }

function that gets the constructor as parameter:

fun function(factory: () -> Animal) {
    val x: Animal = factory()
}

and here is the problem:

    function(::Animal)

How can i solve this conflict? And how function knows which type of constructor it's gets (depends to the constructor, each constructor needs different arguments)





Aucun commentaire:

Enregistrer un commentaire