DSL in Scala for Date Calculation

I’ve been attending a course about Java Architecture in Caelum these last weeks. In one of those Saturdays, the instructor mentioned a little bit about DSLs. Better yet, he gave us an example using Scala =)

Now, the example was intriguing and interesting, at least for a Scala beginner like me, so I decided to translate it to English (the original was in Portuguese) and post it here. If you want, you can see the original in Portuguese, created by Sergio Lopes, here.

Before seeing the translated version of the DSL implementation, lets take a look on how you would use it – which is the most interesting part:

Tomorrow minus 1 month and plus 10 years and plus 1 day

Although the code above looks like a (almost?) proper sentence, it is valid Scala source code. That’s the beauty of writing DSLs in Scala =)

One thing that took me a while to understand regarding this code is the Conjunction part. Represented by the and instance in this case, its purpose is simple (at least after you understand it): pass a partial result to the next part of the calculation, when necessary. Notice in the code how months, years and days have one overloading that receives a Conjunction. This is what makes possible to yield a result that will be passed to the next calculation step.

Here is the full implementation code of the DSL:

import java.util.Calendar

class Date(val data: Calendar) {
 data.clear(Calendar.HOUR)
 import Date.Conjunction

 private var last = 0;

 def plus(num: Int) = { last = num; this }
 def minus(num: Int) = { last = -num; this }

 def +(num: Int) = plus(num)
 def -(num: Int) = minus(num)

 def months = { data.add(Calendar.MONTH, last); this }
 def months(and: Conjunction): Date = months
 def month = months
 def month(and: Conjunction): Date = months

 def years = { data.add(Calendar.YEAR, last); this }
 def years(and: Conjunction): Date = years
 def year = years
 def year(and: Conjunction): Date = years

 def days = { data.add(Calendar.DAY_OF_MONTH, last); this }
 def days(and: Conjunction): Date = days
 def day = days
 def day(and: Conjunction): Date = days

 override def toString = "%1$Td/%1$Tm/%1$TY" format data
}

object Date {
 class Conjunction
 val and = new Conjunction

 def Today = new Date(Calendar.getInstance)
 def Tomorrow = Today + 1 day
 def Yesterday = Today - 1 day

 def today = Today
 def tomorrow = Tomorrow
 def yesterday = Yesterday
}

The only thing I added to the code was operator overloading, so that the usage can be even more interesting, allowing stuff like this:

Today + 2 months

So, the def + and the def – are not present in the original code. I just added those as an exercise to understand how to use operator overload in Scala, which ended up being ridiculously simple. If you want to learn more about operator overloading, Joey Gibson has a nice blog entry about this here.

Advertisements
This entry was posted in scala and tagged , , , , , , . Bookmark the permalink.

11 Responses to DSL in Scala for Date Calculation

  1. It became even better with the operator overload! Congrats! Sergio should update the gist 🙂

  2. OpenDog says:

    Paulo,

    I’m wrecking my brain to understand this Conjunction thing. Can you please shed some more light on it? I’m an experienced Java programmer but newbee in Scala.

    Thanks,
    Dog

  3. OpenDog says:

    Never Mind! I got it!

    So in case of “months”:

    The first call is always months(and). So the method with the Conjunction in the signature is called first. Now the second call from here is the “plain” months.

    Thanks! I loved it!

  4. OpenDog says:

    Ok, last comment …

    The def + and the def – are not present in the original code because it breaks after the “and”. Try it!

    Cheers!

    • Paulo Renato says:

      Hey, thank you for the comments! =)
      I’ll try it out for sure and perhaps write about it later.

    • Paulo Renato says:

      I couldn’t understand exactly why this is happening, but seems to be related to the way Scala tries to infer who is the “owner” of the operator overload to call. I still want to come with a good solution, but if you try this:

      (((Tomorrow – 1 month and) + 10 years and) + 1 day)

      it works. At least, I think this test helps understanding a little bit what is happening.

  5. Pingback: Membuat DSL Tanggal Dengan Scala « Agilinius

  6. Sam says:

    Since “and” is a singleton, why did you make it a “val” rather than an “object”?

    • Paulo Renato says:

      Good point. There is no special explanation for that. I just followed the original code design, while trying to understand how it worked. I added only the + and – definitions – and doesn’t work properly, as noted in some of the comments above.

      Now, thinking about what you said, ‘and’ should probably be an ‘object’ – it would make the code a little bit cleaner.

  7. Matt says:

    Pretty cool, thanks

  8. Pingback: ScalaTest bdd testing using sbt « Charandeep S. Matta

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s