title: iterate over case class data members
body: <p>I am writing a play2.1 application with mongodb, and my model object is a bit extensive. when updating an entry in the DB, i need to compare the temp object coming from the form with what's in the DB, so i can build the update query (and log the changes).</p>
  
  <p>i am looking for a way to generically take 2 instances and get a diff of them. iterating over each data member is long, hard-coded and error prone (if a.firstName.equalsIgnoreCase(b.firstName)) so i am looking for a way to iterate over all data members and compare them horizontally (a map of name -> value will do, or a list i can trust to enumerate the data members in the same order every time).</p>
  
  <p>any ideas?</p>
  
  <p></p>
  
  <pre><code>case class Customer(
    id: Option[BSONObjectID] = Some(BSONObjectID.generate),
    firstName: String,
    middleName: String,
    lastName: String,
    address: List[Address],
    phoneNumbers: List[PhoneNumber],
    email: String,
    creationTime: Option[DateTime] = Some(DateTime.now()),
    lastUpdateTime: Option[DateTime] = Some(DateTime.now())
  )
  </code></pre>
  
  <p>all three solutions below are great, but i still cannot get the field's name, right? that means i can log the change, but not what field it affected...</p>