Demystifying DSLs

Source | Tests

Micro Languages

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