Diese Präsentation wurde erfolgreich gemeldet.
Wir verwenden Ihre LinkedIn Profilangaben und Informationen zu Ihren Aktivitäten, um Anzeigen zu personalisieren und Ihnen relevantere Inhalte anzuzeigen. Sie können Ihre Anzeigeneinstellungen jederzeit ändern.
Nächste SlideShare
What to Upload to SlideShare
What to Upload to SlideShare
Wird geladen in …3
1 von 20

Writing a DSL for the Dense with Scala - JVMCon



Herunterladen, um offline zu lesen

Slides for our talk at JVMCon. Code for the example is available at: https://github.com/NRBPerdijk/dsl-for-the-dense

Ähnliche Bücher

Kostenlos mit einer 30-tägigen Testversion von Scribd

Alle anzeigen

Writing a DSL for the Dense with Scala - JVMCon

  1. 1. WRITING A DSL FOR THE DENSE Nathan Perdijk (@nrbperdijk) Jan-Hendrik Kuperus (@jhkuperus)
  2. 2. Maths / CS / History / Philosophy DSL for Mortgage Calculations Authors of ScalaRules Board Game Geeks JAN-HENDRIK NATHAN
  3. 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. 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. 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. 6. DON'T MAKE A DSL ... Just because you can ... If API changes frequently
  7. 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. 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. 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. 10. EXAMPLES SPEAK LOUDER ... Simple Work ow System Business Rules determine validity of actions Multiple ways to achieve success
  11. 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. 12. MODELING THE BUSINESS Cargo: Wolf, Sheep, Cabbage Activities: Enter boat, Cross the stream, Exit boat Outcomes: Cargo eaten, No eating at all
  14. 14. WHAT'S NEXT Scala Meta offers greater Power Check out Scala-Rules (2.11 only) https://github.com/NRBPerdijk/dsl­for­ the­dense
  15. 15. SCALA MACROS/META/... Compile-time methods AST Transformations Highly experimental
  16. 16. SCALA-RULES Forward-chaining rule engine DSL to express data ow Scala Macros & Meta annotations (so only works in 2.11)
  17. 17. WRITING A DSL FOR THE ...