$30 off During Our Annual Pro Sale. View Details »

Presentation - DSLs in Kotlin by Niranjan Ratnakar

Presentation - DSLs in Kotlin by Niranjan Ratnakar

Presentation slides for SD Kotlin's July 2018 meeting

SD Kotlin

July 11, 2018
Tweet

More Decks by SD Kotlin

Other Decks in Programming

Transcript

  1. Domain Specific Languages
    (DSLs) in Kotlin
    Niranjan Ratnakar

    View Slide

  2. Outline

    What are DSLs?

    Why should we use DSLs?

    Kotlin features to enable DSLs

    Walk through of type safe HTML builder

    View Slide

  3. What are DSLs?

    [Wikipedia definition]

    A domain-specific language (DSL) is a
    computer language specialized to a particular
    application domain.

    This is in contrast to a general-purpose
    language (GPL), which is broadly applicable
    across domains.

    View Slide

  4. What are DSLs?

    A DSL is like an electric drill: it is a powerful
    tool with a wide variety of uses, but a specific
    context, namely, putting holes in things.

    A GPL is a complete workbench, with a variety
    of tools intended for performing a variety of
    tasks.

    View Slide

  5. Another example of DSLs

    Recently, companies are working on AI
    processors

    AI can be done by CPUs and GPUs

    Why do we need custom AI processors?
    – Power efficiency
    – Speed

    View Slide

  6. Examples

    Regular expressions

    SQL

    Android layouts (?)
    – Implemented traditionally in XML
    – Inflated by Android framework at runtime

    View Slide

  7. Why use DSLs?

    Code looks closer to domain description. It is at
    a higher level of abstraction.

    Lesser cognitive load for domain experts to
    write and maintain code.

    The compiler (if applicable) can provide
    assistance while writing code.
    – We will see this in the type safe builder
    example

    View Slide

  8. Why use DSL? (Advanced)

    We wont go into much details on this topic.

    Interpreter design pattern
    – Write a program description using a
    restricted set of primitives
    – Interpret the program separately

    Multiple interpretations for a single program

    Example, evaluate an expression tree or print it

    View Slide

  9. Kotlin support for DSLs

    Write DSLs with Kotlin as the host language

    Programs look closer to the domain

    Can use language constructs
    – for, if, ...

    Example, Anko for android layouts
    – Better looking layout
    – Easier data binding

    View Slide

  10. Anko

    View Slide

  11. Kotlin features to enable DSLs

    Extension functions

    http://kotlinlang.org/docs/reference/extensions.html

    Lambda with receivers

    http://kotlinlang.org/docs/reference/lambdas.html

    Passing a lambda to the last parameter

    https://kotlinlang.org/docs/reference/lambdas.html

    View Slide

  12. View Slide

  13. View Slide

  14. View Slide

  15. View Slide

  16. Type safe HTML builders

    http://kotlinlang.org/docs/reference/type-safe-
    builders.html

    https://try.kotlinlang.org/#/Examples/Longer
    %20examples/HTML%20Builder/HTML
    %20Builder.kt

    View Slide

  17. Walkthrough

    Specify the HTML document as a DSL
    – (Work with a subset of HTML tags)
    – Can have tags that depend on command
    line arguments

    Covert the HTML document to a string

    Use StringBuilder for efficiency

    Lets go to code!

    View Slide