0

Suppose I have the following case class:

object Example extends App {
  case class Cat(name: String, age: Int)
  
  val bob = Cat("Bob", 42)
  
  println(Cat.getClass.getFields)
  println(bob.getClass.getFields)
}

Is there a nice way I can get the field names and the values?

Something like:

Map(name -> "Bob", age -> 42)
finite_diffidence
  • 893
  • 2
  • 11
  • 20
  • 2
    Yes, there are ways o get this. But, the real question would be why? You would lose all type safety. This mostly smells like **JSON**. – Luis Miguel Mejía Suárez Jun 30 '20 at 16:55
  • Thanks @LuisMiguelMejíaSuárez, I am indeed looking to construct something akin to JSON - i.e. a map which I can manipulate and transform. But if it comes at the expense of type safety I will skip it. Out of interest, how can one do this as an academic exercise disregarging type safety? – finite_diffidence Jun 30 '20 at 17:09
  • 2
    The `Map()`, as you've described it, is type `Map[String,Any]` which means you've painted yourself into a corner. `bobMap("name")` is type `Any` so you can't do simple things like get the string length because it's not a `String` it's an `Any`. Likewise, `bobMap("age")` is type `Any` so you can't do simple things like test if > 9 because it's not an `Int` it's an `Any`. (BTW, that's one _old_ cat you got there.) – jwvh Jun 30 '20 at 17:16
  • 1
    Which kind of transformations do you want to make that require a dynamic data structure like a **Map[String, Any]** that you can't do with the normal case class? Understanding all the requisites and constraints is essential to provide the best solution. – Luis Miguel Mejía Suárez Jun 30 '20 at 17:17
  • Thanks, I think the transformations are no longer feasible. As @jwvh said in his comment, I have indeed cornered myself. I think the best solution may be to go back to the drawing board in this case. Thanks guys appreciate the experienced advice!! – finite_diffidence Jun 30 '20 at 17:32
  • @finite_diffidence if you can describe de transformations you want to make we may point you to the right direction. – Luis Miguel Mejía Suárez Jun 30 '20 at 17:33
  • @LuisMiguelMejíaSuárez, the transformations would be type dependent. For example, in some instances I would have a field with type List[CustomType], on which I would call mylist.map(element => elment.insideFunction()). But since I lose type safety, I imagine this would not work properly – finite_diffidence Jun 30 '20 at 20:01
  • 2
    @finite_diffidence but I do not see how you can't do that with a normal case class? Or do you mean that you want a function that will work for any class as long as that class has a member of the correct type? – Luis Miguel Mejía Suárez Jun 30 '20 at 20:41
  • @LuisMiguelMejíaSuárez, I believe it is the latter. Basically I have a lot of case classes. Each of these case classes takes in some basic types (like String, Int), and also may take some custom types composed of other case classes, such as List[Cat]. I will post a related question as I cannot fit in the example code here due to character limit and the OP has been closed, but the comments on losing type safety was definitely helpful! – finite_diffidence Jul 01 '20 at 11:11
  • 1
    Shapeless might help you here – user Jul 01 '20 at 15:22

0 Answers0