Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Building DSLs in Kotlin for Fun and Profit

Sponsored · Your Podcast. Everywhere. Effortlessly. Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.

Building DSLs in Kotlin for Fun and Profit

Talk given at Milan Kotlin Community Conference.
Talk includes a demo, the sample can be found here: https://github.com/zmarkan/kurl

Avatar for Zan Markan

Zan Markan

June 14, 2018
Tweet

More Decks by Zan Markan

Other Decks in Programming

Transcript

  1. @zmarkan @pusher •Adapting the language to suit a domain •Allowing

    non-devs to read code •Making the API neater DSLs are for
  2. @zmarkan @pusher + Save time on repetitive things + Improve

    readability - Learning curve & maintainability of DSL - (Can cause) unintended side-effects
  3. @zmarkan @pusher •Adapting the language to suit a domain •Allowing

    non-devs to read code •Making the API neater •Job security DSLs are for
  4. @zmarkan @pusher Internal DSLs live inside the host language External

    have fewer boundaries Depends on your “domain” Internal vs External DSLs
  5. @zmarkan @pusher • Text processing - awk/sed • Testing -

    gherkin / hamcrest / *spec • Configuration - make, rake, gradle • Visual, statistic, etc…
  6. @zmarkan @pusher • View creation - Anko / hotlinx.html •

    Build definition - gradle.kts • Injection - Koin • …
  7. @zmarkan @pusher • Function Literals with receiver • Infix functions

    • Operator overloading • @DslMarker annotation
  8. @zmarkan @pusher Instantiate function types with literals Receiver itself providing

    the receiver object Common way to “enter” the DSL Literals with receiver
  9. @zmarkan @pusher Call methods without the . and ()
 object.method(argument)

    becomes
 object method argument. Infix notation
  10. @zmarkan @pusher Override +, -, /, (), and others to

    add arithmetic-like methods to classes Operator overloading
  11. @zmarkan @pusher Override +, -, /, (), and others to

    add arithmetic-like methods to classes …or with arithmetic ones, if you like to watch the world burn Operator overloading
  12. @zmarkan @pusher •Avoid children from accessing methods on the parent

    receiver •Limit scope for what can be called •Compiler will enforce it @DslMarker
  13. @zmarkan @pusher •Kotlin gives you tools for building DSLs •DSLs

    can make APIs more readable •Configuration, testing are the top use-cases •Avoid anything “too” crazy Summary