title: How can you write a power Enum with visitor semantics in Scala?
body: <p>There is a pattern I use often to make code simpler that I like to call a Power Enum which is an enumeration that takes advantage of the visitor pattern as well as carries custom data. Here is an example in Java: </p>
  
  <pre><code>import java.security.SecureRandom;
  import java.util.UUID;
  import java.util.stream.IntStream;
  
  public class PowerEnumDemo {
      private static final SecureRandom random = new SecureRandom();
  
      public static void main(String[] args) {
          for (int i = 0; i &lt; 3 ; i++) {
              final Color selected = Color.values()[random.nextInt(3)];
              System.out.println("Selected: " + selected);
              System.out.println("rgb: " + selected.rgb);
              selected.visit();
          }
      }
  
      public enum Color {
          RED("0000ff") {
              @Override
              public void visit() {
                  IntStream.range(1, 10).forEach(System.out::println);
              }
          },
          GREEN("00ff00") {
              @Override
              public void visit() {
                  System.out.println(UUID.randomUUID().toString().chars().filter(c -&gt; c == 'a').count());
                  ;
              }
          },
          BLUE("ff0000") {
              @Override
              public void visit() {
                  System.out.println("Hello World");
              }
          };
          final String rgb;
  
          Color(final String rgb) {
              this.rgb = rgb;
          }
  
          /**
           * This is where the enum implements the visitor pattern to do special stuff.
           */
          public abstract void visit();
      }
  }
  </code></pre>
  
  <p>If one runs this little program you will see how it works. We don't have to know which Enum is selected and we don't have to do any if loops which can be annoying to maintain. If a dev adds a new enum they have to define its data and implement the visit method or the code wont compile. But when they do it will work for everyone calling visit.</p>
  
  <p>I want to implement this in Scala but I am at a loss of how to proceed. Here is the proscribed method of creating an enum which is a tad obscure: </p>
  
  <pre><code>object Color extends Enumeration{
    type Color = Value
    val RED, GREEN, BLUE = Value
  }
  </code></pre>
  
  <p>What I don't see is how I can define my custom data and my abstract visit method and have each val implement the visit method. (for those that don't get it in the java example each enum is kind of a subtype of the main Color enum.) So any of you scala gurus can get this working? :-)</p>