Get field names list from case class

后端 未结 6 1719
别跟我提以往
别跟我提以往 2020-11-27 03:31

I need to get only field names of case class. I\'m not interested in its values. I thought getClass.getDeclaredFields.map(_.getName) would return a list of fiel

6条回答
  •  野性不改
    2020-11-27 04:21

    Starting Scala 2.13, case classes (which are an implementation of Product) are now provided with a productElementNames method which returns an iterator over their field's names.

    From an instance of the case class (let's say case class Person(name: String, age: Int)), one can retrieve a List of its fields:

    Person("hello", 28).productElementNames.toList
    // List[String] = List(name, age)
    

提交回复
热议问题