2011-07-08

Bash/Emacs Rename Refactoring

In my last post I was talking about using Emacs as an IDE for Scala. I imagine you might have a range of objections to such a thing. For instance, what about renaming a class? Between bash and Emacs, it's easy enough. Suppose I want to rename a class from BeezleBopple to BoppleBeezle. Assuming I am confident that there are no occurrences of the text "BeezleBopple" in my code that is not a reference to this class, (this is never really a problem for me, as I tend to name my classes things like CurrentBatchSelectionChangedEvent), I can perform the following steps.

Scala, Emacs and Etags

I've been using Emacs exclusively as my Scala IDE for the last few months. The emacs major mode for Scala is pretty good. It comes with the standard Scala distribution under misc/scala-tool-support/emacs. To load this in to your Emacs environment, I put the following two lines in my .emacs file:
(add-to-list 'load-path "/path/to/scala/misc/scala-tool-support/emacs")
(require 'scala-mode-auto)
I have a couple of gripes about formatting issues, and I may look at "fixing" them some time, but they are very small things, and I can work around them.

UPDATE: The Emacs major mode for Scala is now provided in a separate scala-tool-support archive from the downloads page. Also, a special thanks to whoever fixed the gripes about formatting issues I had!

2011-07-01

Scala Enums: Real Life Example

I wanted to follow up my previous post on Scala Enums with a real-life example. The beauty of this example is my ability to use advanced typing techniques such as type parameters, mixin traits, even a composite pattern, in what basically amounts to a simple enumeration type. This code is quite easy to read, and perfectly conveys my intentions. I don't even want to imagine what I would have done in Java for something like this.

2011-06-27

Scala Enums

A post to the scala-users mailing list was asking about how to do enums. While Java enums are relatively lame, they allow me to do simple things such as attach properties to enumerated values, e.g.:

public enum ColumnHeader {
 
  FirstName("First Name", true),
  LastName("Last Name", true),
  Age("Age", false);
 
  private final String headerText;
  private final boolean visibleByDefault;
 
  private ColumnHeader(headerText, visibleByDefault) {
    this.headerText = headerText;
    this.visibleByDefault = visibleByDefault;
  }
 
  public String getHeaderText() {
    return headerText;
  }
 
  public boolean isVisibleByDefault() {
    return visibleByDefault;
  }
}
 
Java provides a static method values() in your enum class that returns an array of your enum values, in the order you defined them. This is nice. Now you can do things like this:
 
public enum ColumnHeader {
  //...
  public static ColumnHeader[] getVisibleByDefaultColumnHeaders() {
    final List<ColumnHeader> visibleColumnHeaders =
      new ArrayList<ColumnHeader>();
    for (ColumnHeader header: values()) {
      if (header.isVisibleByDefault()) {
        visibleColumnHeaders.add(header);
      }
    }
    return visibleColumnHeaders.toArray(
      new ColumnHeader[visibleColumnHeaders.size());
  }
}

People switching from Java to Scala may want to do something like this, and may well find themselves expecting something similar from the scala.Enumeration class. But it's pretty hard to figure out how to extend scala.Enumeration and add properties to the extended class as well. I've tried a couple times, and I haven't figured it out. Instead, I've taken to using case objects to solve the kinds of problems that I solved with enums in Java.