vendredi 7 août 2020

In Scala, is it possible to instantiate an object of generic type T?

In Scala, even if the solution is not elegant, is it possible to instantiate/create a new object of a generic type T? Is it possible to achieve this using reflection?

For example, I am interested in something like the following:

    case class Person(name: String, age: Int)

Let's say I wanted to do the following to create an object of type Person:

    def createObject[T](fieldValues: Seq[Any]): T = {
        ... T(fieldValues)
    }

    val person = createObject[Person](Seq("Bob", 20))




Aucun commentaire:

Enregistrer un commentaire