- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
DSLs - A powerful Scala feature
Created by bagwell on 2009-04-08.
Updated: 2009-04-09, 10:07
Domain Specific Languages (DSL) written in Scala have become powerful tool in the hands of Scala programmers. In the original language design great care was taken to ensure that the syntax would allow programmers to create natural looking DSLs. Spend a moment to enjoy Michael Fogus's humour and the clever use he makes of implicits to create "Baysick". It looks like BASIC feels like BASIC but it's a Scala DSL. Then you may like to follow some other links to see DSL examples for Financial Asset Management, Apache Camel Services and OSGi.
Michael decided to extend the simple BASIC interpreter posted by Szymon Jachim to the Scala Nabble forums. He says "The exercise was an interesting way to appreciate the power that Scala provides in facilitating DSL creation."
Here is a simple Lunar Lander game he has written using his BASIC-like DSL. And it really runs!
More information can be found on his site and the source code is located at Github.
object Lunar extends Baysick { def main(args:Array[String]) = { 10 PRINT "Welcome to Baysick Lunar Lander v0.9" 20 LET ('dist := 100) 30 LET ('v := 1) 40 LET ('fuel := 1000) 50 LET ('mass := 1000) 60 PRINT "You are drifting towards the moon." 70 PRINT "You must decide how much fuel to burn." 80 PRINT "To accelerate enter a positive number" 90 PRINT "To decelerate a negative" 100 PRINT "Distance " % 'dist % "km, " % "Velocity " % 'v % "km/s, " % "Fuel " % 'fuel 110 INPUT 'burn 120 IF ABS('burn) <= 'fuel THEN 150 130 PRINT "You don't have that much fuel" 140 GOTO 100 150 LET ('v := 'v + 'burn * 10 / ('fuel + 'mass)) 160 LET ('fuel := 'fuel - ABS('burn)) 170 LET ('dist := 'dist - 'v) 180 IF 'dist > 0 THEN 100 190 PRINT "You have hit the surface" 200 IF 'v < 3 THEN 240 210 PRINT "Hit surface too fast (" % 'v % ")km/s" 220 PRINT "You Crashed!" 230 GOTO 250 240 PRINT "Well done" 250 END RUN } }
If you are new to Scala or to DSLs you may like to watch Bill Venners video "The Feel Of Scala" where he both talks about Scala the language and DSLs in the context of program testing.
Debasish Ghosh shows you how he has constructed a DSL to build an Asset Management System in a Financial Trading and Settlement environment.
Heiko Seeberger shows you how you can use a DSL to simplify OSGi implementations.
The Camel project on Apache uses DSL to drive services. See the development team presentation and their implementation
In the academic world DSL are being considered for improving parallelism in multi-core concurrent systems. In this presentation "Towards Pervasive Parallelism" by Kunle Olukotun at Stanford UT Austin, you can read about the general problem of concurrency and the role that DSLs can play. While at Maquarie University you can learn about Scala DSLs in the context of the Kiama project.
- Login or register to post comments
- Printer-friendly version