Tag Archives: date

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.


Store date attributes as primitive longs?

I was reading the item 24 of this book and, in one of the last paragraphs, the author comments about storing date objects as a primitive long. The text is about making defensive copies of objects in your constructors and accessor methods.

So instead of


public void setDate(Date date) {

    this.date = date;

}

you should use


public void setDate(Date date) {

    this.date = new Date(date.getTime());

}

This is pretty obvious after years developing O.O. software, and explaining it is out of the scope of this post. Now, about using primitive longs. It actually seems to be a very interesting idea. Since you would have to use the long value of the local (private) date all the time to create the defensive copies, storing it as a primitive long right away would get us more clean and readable code.

The constructor would simply store the long date, and the acessor would be as simple as


public Date getDate() {

    return new Date(myDateAsLong);

}

Also, it would be easier to store the date in the database, since we wouldn’t need to convert a Date object to a format the database understand.

Now, the bigger question: why didn’t I think about this before??


Follow

Get every new post delivered to your Inbox.