NFJS Day 2: DSLs in Scala External and Internal

Looking to expand my scala skills, for the second sessions I decided to attend Michael Nygard’s session on Internal and External DSLs in scala. What I really liked was right off the bat how he illustrated a very simple example of internal DSLs in scala for those new to scala and DSLs:

def isComment(s : String) = s.startsWith("#")

which could also be done with no puncutation

def isComment(s : String) = s startsWith "#"

But that was just the simplistic starting point. The real world example that he spent a good chunk of the session tearing apart looked like this:

apache offers "HTTP" on 80
apache offers "HTTPS" on 443

apache contains "mod_cm" needing "CMLDRP"

The above is actually real scala code that can be compiled and ran by scala! The trick is a nifty combination of factory methods, implicit functions, and scala’s ability to have punctuation optional. Combining all of these plus more together yielded a nice, higher level DSL like the one above. He then showed the dsl being used within specs as an example.

Moving on, external DSLs were covered and how to create a grammar parser using the building blocks that scala provides and how to use trait parsers to aid in parsing a grammar. Combinators played a big part in tall of this and creating a tree of combinators. leading from this in depth explaination, he showed how to use Parsers.Parser for parsing the structure.

I really enjoyed the session… I even got some of the examples he showed using combinators working on my laptop. There’s definitely a lot of new scala knowledge for me to digest! :)

You can leave a response, or trackback from your own site.

Facebook comments:

2 Responses to “NFJS Day 2: DSLs in Scala External and Internal”

  1. Eric says:

    Hi James,

    Interesting, I’ll also be giving a talk at OSCON 2010 featuring a short tour of specs and tips and tricks for internal DSLs with Scala. Would you, by any chance, have a link to Michael’s presentation?

    Thanks.

  2. James Carr says:

    Eric,

    I’ll take a look and see if he made the session slides available online somewhere. If not, you can always contact him on twitter (@mtnygard), I’m sure he’d be glad to help out. ;)

Leave a Reply

Subscribe to RSS Feed Follow me on Twitter!