title: Scala case class copy constructor with dynamic fields
body: <p>I have an immutable <code>State</code>, and I process a message queue, where each message is a list of new values to some fields in the state.</p>
  
  <p>The new values may apply to a part of a field - for example setting or clearing one flag (bit) out of many, or changing just the low or high 8-bit part of a 16-bit field.</p>
  
  <p>After processing the message, I want to get an immutable copy of the state, with the modifications applied.</p>
  
  <pre><code>object StateField {
    sealed abstract class StateField()
    sealed abstract class Register extends StateField
    sealed abstract class Flag extends StateField
    case object AX extends Register
    case object AH extends Register
    case object AL extends Register
    case object CF extends Flag
    case object OF extends Flag
  }
  
  class StateFieldModification(field: StateField, value: Int)
  
  class ModificationMessage(content: List[StateFieldModification])
  
  case class State(AX: Int, Flags: Int) {
  
    def readRegister(field: StateField.Register): Int = field match {
      case StateField.AX =&gt; this.AX
      case StateField.AH =&gt; this.AX &amp; 0xFF
      case StateField.AL =&gt; (this.AX &lt;&lt; 8) &amp; 0xFF
    }
  
    def readFlag(field: StateField.Flag): Boolean = field match {
      case StateField.CF =&gt; (this.Flags &amp; 0x0001) != 0
      case StateField.OF =&gt; (this.Flags &amp; 0x0800) != 0
    }
  
    def flagsWithBit(flagBit: Int, newBitValue: Boolean): Int = {
      if (newBitValue) Flags | (1 &lt;&lt; flagBit) else Flags &amp; ~(1 &lt;&lt; flagBit)
    }
  
    def withModification(modification: StateFieldModification): State = modification.field match {
      case StateField.AX =&gt; this.copy(AX = modification.value)
      case StateField.AH =&gt; this.copy(AX = (this.AX &amp; 0x00FF) | (modification.value &lt;&lt; 8))
      case StateField.AL =&gt; this.copy(AX = (this.AX &amp; 0xFF00) | modification.value)
      case StateField.CF =&gt; this.copy(Flags = flagsWithBit(1, modification.value &gt; 0))
      case StateField.CF =&gt; this.copy(Flags = flagsWithBit(12, modification.value &gt; 0))
    }
  
    def withModifications(message: ModificationMessage) = ???
  }
  </code></pre>
  
  <p><strong>Q#1</strong> - what's the best way to make a field getter based on a "field key"?</p>
  
  <p><strong>Q#2</strong> - what's the best way to make a field setter based on a "field key"?</p>
  
  <p><strong>Q#3</strong> - what's the best way to make a new object, given a mutability message?</p>
  
  <p>Please note that: </p>
  
  <ul>
  <li>I'm experienced with Java but just starting with Scala.</li>
  <li>I'm doing a clean-sheet project, so you're more than welcome to comment about any part of the code, anything can be rewritten.</li>
  <li>The code above works, but it feels too far from what Scala elegance should look like.</li>
  <li>I <em>don't</em> want to use reflection, as suggested here:
  <a href="https://stackoverflow.com/questions/17312254/scala-case-class-copy-with-dynamic-named-parameter">Scala case class copy with dynamic named parameter</a></li>
  <li>I <em>don't</em> want to use key-value map of fields instead of real class fields, like Python does.</li>
  </ul>
  
  <p>All help appreciated. Thanks!!!</p>