Domänenspezifische Sprachen (engl. DSLs) sind seit jeher dazu geeignet, komplexe Ausdrücke kompakter und besser lesbar auszudrücken. Dabei befreien sie den Benutzer von der Notwendigkeit, wiederkehrende Programmfragmente zu pflegen. Sie reduzieren den Blick auf den wesentlichen Inhalt der zugrunde liegenden Domäne. Während es an sinnvollen Anwendungsgebieten von DSLs nicht mangelt, sind deren Funktionsweise und Erstellung oftmals zu Unrecht als Mysterium verschrieen. Domänenspezifische Sprachen unterteilen sich grundsätzlich in externe und interne DSLs. Externe DSLs definieren eine unabhängige eigene Sprache, z. B. Xtend oder SQL. Daher bedarf es zu deren Ausführung einer eigenständigen Syntaxanalyse, -validierung und eines Compilers oder Interpreters. Interne DSLs basieren auf Host-Programmiersprachen. Daher ist ihr Sprachumfang nicht abgeschlossen, sondern lässt sich durch deren Sprachelemente erweitern. Kotlin bietet mit Extension Functions und Lambdas with Receivers ideale Voraussetzungen für die Erstellung interner DSLs. Dies zeigt auch ein Blick auf die ständig wachsende Zahl von Kotlin-basierten DSLs, wie z. B. der Kotlin Gradle DSL oder der Spring Beans DSL. In diesem Vortrag sollen die Grundlagen zur Erstellung eigener DSLs mit Kotlin vermittelt werden. Anhand eines praxisorientierten Beispiels wird schrittweise eine DSL während des Vortrags erstellt.
3. 3
Domain Specific Languages: Definition
“DSLs are small languages, focused on a
particular aspect of a software system.
You can't build a whole program with a DSL,
but you often use multiple DSLs in a
system mainly written in a general purpose
language.”
- Martin Fowler with Rebecca Parsons
https://images-eu.ssl-images-amazon.com/images/I/51qFZ0yIUsL.jpg
4. 4
Domain Specific Languages: Definition
“DSLs come in two main forms: external
and internal. An external DSL is a language
that's parsed independently of the host
general purpose language: good examples
include regular expressions and CSS.”
- Martin Fowler with Rebecca Parsons
https://images-eu.ssl-images-amazon.com/images/I/51qFZ0yIUsL.jpg
5. 5
Domain Specific Languages: Definition
“Internal DSLs are a particular form of API
in a host general purpose language, often
referred to as a fluent interface. The way
mocking libraries, such as JMock, define
expectations for tests are good examples
of this.”
- Martin Fowler with Rebecca Parsons
https://images-eu.ssl-images-amazon.com/images/I/51qFZ0yIUsL.jpg
6. 6
Kotlin Internal DSL Selection
Gradle Kotlin DSL
https://github.com/gradle/kotlin-dsl
Beans/Router/MockMvc DSL
https://github.com/spring-projects/spring-framework
Kotlin Test
https://github.com/kotlintest/kotlintest
Kuery SQL in Kotlin
https://www.kotlinresources.com/library/kuery/
Spring Fu Microframework
https://github.com/spring-projects/spring-fu
GeoJSON Builder DSL
https://github.com/dxfrontiers/kotlin-geojson-dsl
7. 7
1. Predictable
Domain logic is expressed declaratively, while execution is controlled by the DSL.
2. Safer
Restricted dialect and clearer scoping reduces unsafe operations.
3. More expressive
Domain code clearly expresses what should be done. Domain specific errors are easier to understand.
4. Portable
Domain logic execution can be ported to new platforms, if necessary.
5. Easier to understand
Domain experts are able to learn the DSL in less time.
DSL Advantages
8. 8
Building Blocks for DSLs in Kotlin 1/2
Simplified Lambda Syntax
Extension Functions
10. 10
A Gherkin Test DSL
“Gherkin uses a set of special
keywords to give structure
and meaning to executable
specifications.”
- Cucumber
GIVEN
state before
behavior is
applied
WHEN
specifies the
behavior part
THEN
defines changes
or expected
outcome
12. 12
The Calculator: Gherkin-Style Verification
“given”-Lambda
yields the test
subject
“when”-Lambda receives
test subject and returns
test execution result
“then”-Lambda receives
an assertion object
wrapping the test result
22. 22
Kotlin DSL Building Blocks
1. Capturing declarations/logic using Lambdas
2. Providing Declarative Context using Lambdas with Receiver
3. Providing scoped (extension) functions
4. Improving Readability using infix notation
5. (Operator overloading)
23. 23
What’s the next DSL you’ll be designing?
Sprache als Werkzeug
DSLs mit Kotlin
https://github.com/dxfrontiers/kotlin-gherkin-dsl/tree/kkon
https://www.heise.de/developer/artikel/Sprache-als-Werkzeug-DSLs-mit-Kotlin-bauen-4599797.html
https://blog.digitalfrontiers.de
@maverick_1601
https://github.com/maverick1601
https://bit.ly/2EDAagi
Frank Scheffler