Previous slide
Next slide
Toggle fullscreen
Toggle overview view
Open presenter view
Demystifying DSLs
Source
|
Tests
Micro Languages
Microservices “do one thing”
Good functions “do one thing”
Unix programs do one thing
DSLs do less than general purpose
Legacy of Language
Domain Specific Languages (DSLs) are
everywhere
CSS
HTML
SQL?
Terraform
But Why?
Less mental overhead
Given to non-programmers to solve programming problems
Specificity = simplicity and simplicity = convenience
Kotlin DSLs
Major focus of Kotlin
Typesafe builders
Function Literals with Receivers
Lambdas
Anonymous functions
Can be assigned to a variable
Can be passed around
theme: basic-dark
transition: fade
Named Lambdas
Can be given parameters
Not different from statically compiled functions
Extensions
Act as if the function existed on the base object
Imported into files
The parameter becomes the ‘receiver object’
Access to ‘this’
Let Also Apply
Generic extension functions
More on these later
Mapping Things Out
Let’s combine an extension function with a passed in lambda
Let’s make it generic
Let’s remove the parentheses and just use the curly brackets
Let Also Apply - A Second Look
Generic extension functions
Also and Let take the generic of the object we’re calling on
Apply uses a receiver
R.()
Lambda that instead of taking a parameter, is setting R as the receive, and so the lambda has access to the ‘this’ of R
A Basic DSL
Further Play
Kotlin Conf DSLs
Building a basic DSL
HTML Builder
DSLs in Quest Command