Scala, DSLs, Behavior Driven Development?

I’ve started looking at Scala recently because it seems exactly the kind of language I would like to use as a complement to Java on the JVM: it’s similar to Java, but is a “pure” object oriented language and supports a shorter syntax, annotations, generics, closures, a functional programming style and mix-ins. Oh, and it’s fast, or at least seems to be from the little information that is available.

The fact that it’s statically typed is a huge plus in my eyes — I spent two and a half years as a PHP programmer before moving to Java, but think that dynamically typed languages are not for every task. Maybe I’m overgeneralizing, maybe.

Since Scala is rather new and doesn’t have many libraries yet, I’m sure there are plenty of exciting things that can and will be done in it, but I think there are a few areas where Scala should be especially useful. I’m talking about DSLs! Especially DSLs interacting with Java code (and preferably one way).

Trying to implement embedded DSLs in Java just doesn’t work. They are ugly, and even the so-called fluent interfaces in Java are of a somewhat dubious nature. Whenever I look at another piece of code like this …

state( aNonEmptyStack ).should( not( be( empty() ) ) );

… I think, what the hell are these people doing? And this is one of the least ugly examples. What they could be doing instead: writing their libraries in Scala. Since Scala is one of those DSL friendly languages and interoperates with Java easily (at least when Scala code is calling Java code, frankly I’m not sure how well the opposite dependency direction works), it should be awesome to write test/mock/specification libraries in Scala, for testing existing Java code. I can imagine that using those libraries would be a joy.

Since tests are separated from the main code anyway, or should be if you follow good practices, why not write them in a language that makes the code shorter and more readable?

2 thoughts on “Scala, DSLs, Behavior Driven Development?

  1. Well, it seems that such a library for Scala called “specs” has just recently been made public:

    I’m going to give it a try while creating an SQL string builder — my first experiment with writing fluent interfaces in Scala. Here are some example tests for it using specs:

    object SQLBuilderSpecs extends Specification {
      "SQL Builder " should {
        "allow building a SELECT statement" in {
          val sql = new SQLBuilder select "1,2"
          sql.toString mustEqual "SELECT 1,2"
        "allow building a SELECT FROM statement" in {
          val sql = new SQLBuilder select "1" from "DUAL"
          sql.toString mustEqual "SELECT 1 FROM DUAL"

  2. Hi,

    I am the author of specs. I was also very much impressed by Scala, as a Java-compatible, verbose-less language.

    In specs, I am essentially using 2 things to get DSL-like capabilities:

    -implicit definitions to add methods such as “must” to any kind of object (see the article from Martin Odersky:
    (in the example above, “mustEqual x” is an alias to must beEqual(x). I provided a few aliases for frequently used methods)

    -syntactic sugar for methods with one parameter:

    myObject.method(value) myObject method value

    However, Scala still has parenthesis, and I wish sometimes I could write something like:

    def start(v: Any)
    start test

    which is the kind of things you can do with Ruby (but you loose static-type checking,…).

    Don’t hesitate to add questions, comments and wishes on the web site if you experiment with specs.

    I, too, hope there’s a life beyond Java, and I hope that Scala will become a part of it!

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s