Getting public fields (and their respective values) of an Instance in Scala/Java

前端 未结 5 963
耶瑟儿~
耶瑟儿~ 2020-12-20 14:13

PHP introduces a method that allows you to pick out all public values of an instance. Is there any way to do this in Scala? That is to fetch all values of all public fields

5条回答
  •  情书的邮戳
    2020-12-20 15:00

    A per Philippe's answer, you can do this for case classes.

    More broadly though, the same technique works for any subclass of Product. As well as case classes, Tuples are another obvious example, but the list is far more extensive than that.

    Take a look at the "known subclasses", here: http://www.scala-lang.org/api/current/scala/Product.html

提交回复
热议问题