title: Scala: Constraints on fields that are not always respected
body: <p>Give me some help, I just cannot solve this problem.
  How do I ensure that constraints on getter methods are always respected?
  My classes have this form:</p>
  
  <pre><code>abstract class Element {
    var name: String
    var description: String
  }
  
  class Component (var name: String, var description: String) extends Element
  </code></pre>
  
  <p>For the purpose of my project I need to use var.
  I want to put constraints on the field <code>name</code> that must always be respected.
  Both when I create an instance of <code>Component</code> as follows:</p>
  
  <pre><code>val C1 = new Component ("C1 Component", "Description of C1")
  </code></pre>
  
  <p>Both when I change the value:</p>
  
  <pre><code>C1.name = "new value"
  </code></pre>
  
  <p>If I create the classes in this way:</p>
  
  <pre><code>abstract class Element {
    protected var _name : String
    // Getter
    final def name = _name
    // Setter
    final def name_= (value:String):Unit =
      if (value.size &lt; 5) println ("ERROR: Bad Value")
      else _name = value
  
    var description: String
  }
  
  class Component (protected var _name : String, var description: String) extends Element
  </code></pre>
  
  <p>In the main method, because I do not have control here:</p>
  
  <pre><code>val C1 = new Component ("C1", "Description of C1")
  </code></pre>
  
  <p>But only later, when I change the value:</p>
  
  <pre><code>C1.name = "comp"
  </code></pre>
  
  <p>I want my getter to always be respected.
  How do I do this?</p>