title: Implements trait with an anonymous object in Scala
body: <p>I have a trait definition:</p>
  
  <pre><code>trait T {
    def name: String
  }
  </code></pre>
  
  <p>I can create an object like:</p>
  
  <pre><code>val o = new {
    val name: String = "anonymous"
  } with T
  </code></pre>
  
  <p>But I cannot create the object in the following way:</p>
  
  <pre><code>val o = new {
    def name: String = "anonymous"
  } with T
  </code></pre>
  
  <p>The compiler said <strong>';' or newline expected</strong> in line <code>} with T</code>. The only  different is I used <code>def</code> instead of <code>val</code> in the second implementation.</p>
  
  <p>I know that method can be defined in an anonymous object, but why I cannot use in this way here?</p>