title: Scala enum extending trait
body: <p>I am writing a simple parser is Scala.</p>
  
  <p>I have a base trait which represents an element in the file.</p>
  
  <pre><code>trait Token[T] {
      def stringValue: String
      def value: T
  }
  </code></pre>
  
  <p>This is what I need - the string (text) value and the parsed value (which sometimes will the the same string). Now I want to have a set of subclasses for:</p>
  
  <ul>
  <li>reserved symbols / keywords e.g. <code>class</code>, <code>void</code> etc.</li>
  <li>special symbols e.g. <code>+</code>, <code>/</code> etc.</li>
  <li>integer literals e.g. 123</li>
  <li>real literals e.g. 1.23</li>
  <li>string literals e.g. <em>'123'</em></li>
  </ul>
  
  <p>How would you implement a hierarchy like this? Since this is finite it'd be nice to use a case class.. I think. But an enumeration would be great too .. How to combine?</p>
  
  <hr>
  
  <p>In other words, what's the best way to write this (below) in Scala in a more Scal-ish way?</p>
  
  <pre><code>public interface Token&lt;T&gt; {
      String stringValue();
      T value();
  }
  
  public enum ReservedSymbol implements Token&lt;ReservedSymbol&gt; {
      CLASS('class'), VOID('void');
  
      private String val;
      private ReservedSymbol(String val) { this.val = val; }
  
      public String stringValue() { return val; }
      public ReservedSymbol value() { return this; }
  }
  
  
  public class IntegerLiteral implements Token&lt;Integer&gt; {       
      private Integer val;
      public IntegerLiteral(String val) { this.val = Integer.valueOf(val); }
  
      public String stringValue() { return val.toString(); }
      public Integer value() { return val; }
  }
  </code></pre>
  
  <p>etc.</p>