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

Building DSLs in Kotlin for Fun and Profit

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

117c1e7c12a469a052f1361b6599af31?s=128

Zan Markan

June 14, 2018
Tweet

Transcript

  1. @zmarkan @pusher Building 
 DSLs in Kotlin 
 for fun

    and profit
  2. @zmarkan @pusher Pusher Hosted APIs for 
 collaboration & communication

    Channels, Beams, Chatkit pusher.com @pusher
  3. @zmarkan @pusher DSLWTF

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

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

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

    non-devs to read code •Making the API neater •Job security DSLs are for
  7. @zmarkan @pusher Use-Cases

  8. @zmarkan @pusher Internal DSLs live inside the host language External

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

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

    Build definition - gradle.kts • Injection - Koin • …
  11. @zmarkan @pusher • Type-safe Builders • Configuration syntax • Tasks,

    objects, …
  12. @zmarkan @pusher What Kotlin Gives us

  13. @zmarkan @pusher • Type aliases • Extension functions • Lambdas

    • named arguments • default values
  14. @zmarkan @pusher • Function Literals with receiver • Infix functions

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

    the receiver object Common way to “enter” the DSL Literals with receiver
  16. @zmarkan @pusher Literals with receiver

  17. @zmarkan @pusher Call methods without the . and ()
 object.method(argument)

    becomes
 object method argument. Infix notation
  18. @zmarkan @pusher Infix notation

  19. @zmarkan @pusher Override +, -, /, (), and others to

    add arithmetic-like methods to classes Operator overloading
  20. @zmarkan @pusher Operator overloading

  21. @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
  22. @zmarkan @pusher Never EVER do that.

  23. None
  24. @zmarkan @pusher •Avoid children from accessing methods on the parent

    receiver •Limit scope for what can be called •Compiler will enforce it @DslMarker
  25. @zmarkan @pusher Demo - kurl
 Kotlin HTTP DSL

  26. @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
  27. @zmarkan @pusher COM IN G (VERY) SOON

  28. @zmarkan @pusher Zan Markan zan@pusher.com / pusher.com slides: bit.ly/building-kotlin-dsls sample:

    https://github.com/zmarkan/kurl fin
  29. @zmarkan @pusher • https://kotlinlang.org/docs/reference/type-safe-builders.html • https://github.com/zsmb13/VillageDSL • https://speakerdeck.com/writtmeyer/kotlin-dsls • https://javieracero.com/blog/internal-vs-external-dsl

    • https://martinfowler.com/bliki/InternalDslStyle.html Resources