title: Scala: reflection and case classes
body: <p>The following code succeeds, but is there a better way of doing the same thing? Perhaps something specific to case classes? In the following code, for each field of type String in my simple case class, the code goes through my list of instances of that case class and finds the length of the longest string of that field.</p>
  
  <pre><code>case class CrmContractorRow(
                               id: Long,
                               bankCharges: String,
                               overTime: String,
                               name$id: Long,
                               mgmtFee: String,
                               contractDetails$id: Long,
                               email: String,
                               copyOfVisa: String)
  
  object Go {
    def main(args: Array[String]) {
      val a = CrmContractorRow(1,"1","1",4444,"1",1,"1","1")
      val b = CrmContractorRow(22,"22","22",22,"55555",22,"nine long","22")
      val c = CrmContractorRow(333,"333","333",333,"333",333,"333","333")
      val rows = List(a,b,c)
  
      c.getClass.getDeclaredFields.filter(p =&gt; p.getType == classOf[String]).foreach{f =&gt;
        f.setAccessible(true)
        println(f.getName + ": " + rows.map(row =&gt; f.get(row).asInstanceOf[String]).maxBy(_.length))
      }
    }
  }
  </code></pre>
  
  <p>Result:</p>
  
  <pre><code>bankCharges: 3
  overTime: 3
  mgmtFee: 5
  email: 9
  copyOfVisa: 3
  </code></pre>