Slide 1

Slide 1 text

Roll your own DSL with Kotlin A lightning talk by Miquel Beltran Android Developer @ Nebenan.de @Miqubel

Slide 2

Slide 2 text

What’s a DSL? @Miqubel

Slide 3

Slide 3 text

android { compileSdkVersion 26 buildToolsVersion "26.0.2" defaultConfig { … @Miqubel

Slide 4

Slide 4 text

SELECT name FROM users WHERE age >= 30 @Miqubel

Slide 5

Slide 5 text

verticalLayout { padding = dip(32) imageView(…).lparams { margin = dip(16) gravity = Gravity.CENTER } … @Miqubel

Slide 6

Slide 6 text

// Old School Java Style val ratingView = RatingView() ratingView.setStars(3) ratingView.setMaxStars(5) // Kotlin DSL Style rating { initial = 3 maximum = 5 } @Miqubel

Slide 7

Slide 7 text

// Creating our DSL fun rating(func:() -> Unit) // Using our DSL rating({}) @Miqubel

Slide 8

Slide 8 text

// Creating our DSL fun rating(func:() -> Unit) // Using our DSL rating({ }) @Miqubel

Slide 9

Slide 9 text

// Creating our DSL fun rating(func:() -> Unit) // Using our DSL rating { } @Miqubel

Slide 10

Slide 10 text

// Creating our DSL fun rating(func:() -> Unit) // Using our DSL rating { initial = 3 maximum = 5 } @Miqubel

Slide 11

Slide 11 text

// Creating our DSL fun rating(func: RatingDsl.() -> Unit) // Using our DSL rating { initial = 3 maximum = 5 } @Miqubel

Slide 12

Slide 12 text

// Creating our DSL fun rating(func: RatingDsl.() -> Unit) class RatingDsl(var initial: Int = 1, var maximum: Int = 5) // Using our DSL rating { initial = 3 maximum = 5 } @Miqubel

Slide 13

Slide 13 text

// Creating our DSL fun rating(func: RatingDsl.() -> Unit) = RatingDsl().apply { func() } class RatingDsl(var initial: Int = 1, var maximum: Int = 5) // Using our DSL rating { initial = 3 maximum = 5 } @Miqubel

Slide 14

Slide 14 text

// Creating our DSL fun rating(func: RatingDsl.() -> Unit) = RatingDsl() .apply { func() } .build() class RatingDsl(var initial: Int = 1, var maximum: Int = 5) { fun build(): RatingView { return RatingView().apply { setStars(initial) setMaxStars(maximum) } } } // Using our DSL rating { initial = 3 maximum = 5 } @Miqubel

Slide 15

Slide 15 text

Thanks! If you would be interested in a full length talk about this, let me know! @Miqubel