1. WRITING A DSL FOR THE DENSE
Nathan Perdijk (@nrbperdijk)
Jan-Hendrik Kuperus (@jhkuperus)
2. Maths / CS / History / Philosophy
DSL for Mortgage Calculations
Authors of ScalaRules
Board Game Geeks
JAN-HENDRIK NATHAN
3. WHAT IS A DSL ? (1/2)
Formal de nition: A domain-speci c language (DSL)
is a computer language specialized to a particular
application domain. (...) the term "domain-speci c
language" has become more popular due to the rise
of domain-speci c modeling. (Wikipedia, consulted
19 January 2018)
4. WHAT IS A DSL ? (2/2)
Our de nition: Providing easy to use and/or
aesthetically pleasing ways to access a (public) API to
improve its domain expressiveness and expand its
potential userbase.
5. PURPOSES OF A DSL
Abstract initialisation logic away
Increase readability of code
Guide unfamiliar users through an API
Increase 'con gurability' of a library
6. DON'T MAKE A DSL ...
Just because you can ...
If API changes frequently
7. WHY USE SCALA?
Optional dots and parenthesis element toString
Implicit conversions 4
In x Type Constructor new (Dollar Per Hour):
Per[Dollar, Hour]
Singleton Instances
Code Completion in IntelliJ
Macros ...
. ()
intToString( )
8. DESIGNING A DSL
Determine your ubiquitous language
Find your expressive freedom
Fit it into a 'rhythm'
Decide what your DSL returns
Lots of trial and error
9. FITTING THE RHYTHM
Basic premise: use odd numbers of words
Hello.there(JVMCon).how(do).you(do)
object Hello { def there(JVMCon.type): HelloThere }
class HelloThere { def how(do.type): HowPart }
class HowPart { def you(do.type): String }
object JVMCon {}
object do {}
10. EXAMPLES SPEAK LOUDER ...
Simple Work ow System
Business Rules determine validity of actions
Multiple ways to achieve success
11. WOLF & SHEEP VEGETABLE LOGISTICS LTD.
Cabbage and Animal Transport across streams
Some issues in Startup Phase
Shipments were eaten
Locally sourced transport (really tiny boat)
12.
13.
14.
15. MODELING THE BUSINESS
Cargo: Wolf, Sheep, Cabbage
Activities: Enter boat, Cross the stream, Exit boat
Outcomes: Cargo eaten, No eating at all