Slide 1

Slide 1 text

DSL DSL DSL fun with{ } @inyaki_mwc

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

Why?

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

DSL

Slide 6

Slide 6 text

DSL Internal External

Slide 7

Slide 7 text

DSL Internal External

Slide 8

Slide 8 text

DSL Internal External https://github.com/gradle/kotlin-dsl

Slide 9

Slide 9 text

DSL Internal External

Slide 10

Slide 10 text

Semantic Model

Slide 11

Slide 11 text

Semantic Model Domain Model !=

Slide 12

Slide 12 text

Internal DSL

Slide 13

Slide 13 text

Internal DSL Expression Builder Function Sequence Nested Clousure

Slide 14

Slide 14 text

Anko

Slide 15

Slide 15 text

Kakao

Slide 16

Slide 16 text

Why?

Slide 17

Slide 17 text

infix

Slide 18

Slide 18 text

description.should(endWith(value))

Slide 19

Slide 19 text

description should endWith(value) description.should(endWith(value))

Slide 20

Slide 20 text

description should endWith(value) description.should(endWith(value))

Slide 21

Slide 21 text

description should endWith(value) description.should(endWith(value)) infix fun T.should(matcher: Matcher)

Slide 22

Slide 22 text

description should endWith(value) description.should(endWith(value)) infix fun T.should(matcher: Matcher)

Slide 23

Slide 23 text

https://github.com/kotlintest

Slide 24

Slide 24 text

https://github.com/kotlintest public infix fun T.should(matcher: (T) -> kotlin.Unit): kotlin.Unit { } public infix fun T.shouldBe(any: U?): kotlin.Unit { } public infix fun T.shouldHave(matcher: io.kotlintest.Matcher): kotlin.Unit { } public infix fun T.shouldNot(matcher: io.kotlintest.Matcher): kotlin.Unit { } public infix fun T.shouldNotBe(any: kotlin.Any?): kotlin.Unit { }

Slide 25

Slide 25 text

https://github.com/kotlintest public infix fun T.should(matcher: (T) -> kotlin.Unit): kotlin.Unit { } public infix fun T.shouldBe(any: U?): kotlin.Unit { } public infix fun T.shouldHave(matcher: io.kotlintest.Matcher): kotlin.Unit { } public infix fun T.shouldNot(matcher: io.kotlintest.Matcher): kotlin.Unit { } public infix fun T.shouldNotBe(any: kotlin.Any?): kotlin.Unit { } class MyTests : StringSpec() { init { "length should" { "hello".length shouldBe 5 } } } class Example: StringSpec() { init { "String size" { assertAll { a: String, b: String -> (a + b) should haveLength(a.length + b.length) } } } }

Slide 26

Slide 26 text

data class Salary(private val quantity: Double) { infix fun taxes(amount: Double): Salary { return Salary(quantity - amount) } }

Slide 27

Slide 27 text

data class Salary(private val quantity: Double) { infix fun taxes(amount: Double): Salary { return Salary(quantity - amount) } } fun netSalary(): Salary { val mySalary = Salary(1000.00) return mySalary taxes 200.00 }

Slide 28

Slide 28 text

I am your Father Luke

Slide 29

Slide 29 text

I am your Father Luke Demo

Slide 30

Slide 30 text

lambdas { x, y -> x + y }

Slide 31

Slide 31 text

lambdas Function Types High order Functions Lambdas Expressions

Slide 32

Slide 32 text

Function Types

Slide 33

Slide 33 text

Function Types () -> Unit

Slide 34

Slide 34 text

Function Types () -> Unit (View) -> Unit

Slide 35

Slide 35 text

Function Types () -> Unit (View) -> Unit (String) -> Int

Slide 36

Slide 36 text

Function Types () -> Unit val viewResult : () -> Unit

Slide 37

Slide 37 text

Function Types (View) -> Unit fun drawView(view: (View) -> Unit)

Slide 38

Slide 38 text

Function Types (String) -> Int fun maximVolume() : (String) -> Int

Slide 39

Slide 39 text

lambdas expressions val sum: (Int, Int) -> Int = { x, y -> x + y }

Slide 40

Slide 40 text

lambdas expressions val sum: (Int, Int) -> Int = { x, y -> x + y } val sum = { x: Int, y: Int -> x + y }

Slide 41

Slide 41 text

lambdas expressions val sum: (Int, Int) -> Int = { x, y -> x + y } val sum = { x: Int, y: Int -> x + y } fun formula(a: Double, f: (Double) -> Double): Double = f(a)

Slide 42

Slide 42 text

lambdas expressions val sum: (Int, Int) -> Int = { x, y -> x + y } val sum = { x: Int, y: Int -> x + y } fun formula(a: Double, f: (Double) -> Double): Double = f(a) val value = formula(3.0, { i: Double -> sqrt(i) })

Slide 43

Slide 43 text

lambdas expressions val sum: (Int, Int) -> Int = { x, y -> x + y } val sum = { x: Int, y: Int -> x + y } fun formula(a: Double, f: (Double) -> Double): Double = f(a) val value = formula(3.0, { i: Double -> sqrt(i) }) val value = formula(3.0, { sqrt(it) })

Slide 44

Slide 44 text

lambdas expressions val sum: (Int, Int) -> Int = { x, y -> x + y } val sum = { x: Int, y: Int -> x + y } fun formula(a: Double, f: (Double) -> Double): Double = f(a) val value = formula(3.0, { i: Double -> sqrt(i) }) val value = formula(3.0, { sqrt(it) }) val value = formula(3.0) { sqrt(it) }

Slide 45

Slide 45 text

lambdas Demo

Slide 46

Slide 46 text

lambda with receiver

Slide 47

Slide 47 text

return with(Salary()) { gross = 1000.00 taxes = 200.00 pension = 100.00 calculate() } lambda with receiver

Slide 48

Slide 48 text

return with(Salary()) { gross = 1000.00 taxes = 200.00 pension = 100.00 calculate() } lambda with receiver fun with(receiver: T, block: T.() -> R): R = receiver.block()

Slide 49

Slide 49 text

salary { calculate() }

Slide 50

Slide 50 text

class Salary { fun calculate() {} } salary { calculate() }

Slide 51

Slide 51 text

fun salary(f: Salary.() -> Unit): Salary { val salary = Salary() salary.f() return salary } class Salary { fun calculate() {} } salary { calculate() }

Slide 52

Slide 52 text

class Order(val buy: Buy)

Slide 53

Slide 53 text

class Order(val buy: Buy) class Buy(var price: Double, var stopLoss: Double, var marginProfit: Double)

Slide 54

Slide 54 text

class Order(val buy: Buy) class Buy(var price: Double, var stopLoss: Double, var marginProfit: Double) fun order(f: Order.() -> Unit): Order.() -> Unit = f

Slide 55

Slide 55 text

class Order(val buy: Buy) class Buy(var price: Double, var stopLoss: Double, var marginProfit: Double) fun order(f: Order.() -> Unit): Order.() -> Unit = f

Slide 56

Slide 56 text

class Order(val buy: Buy) class Buy(var price: Double, var stopLoss: Double, var marginProfit: Double) fun order(f: Order.() -> Unit): Order.() -> Unit = f

Slide 57

Slide 57 text

order { buy.price = 100.00 buy.marginProfit = 105.00 buy.stopLoss = 98.00 }

Slide 58

Slide 58 text

order { buy { price = 100.00 marginProfit = 105.00 stopLoss = 98.00 } }

Slide 59

Slide 59 text

fun order(f: Order.() -> Unit): Order.() -> Unit = f fun buy(f: Buy.() -> Unit)= {}

Slide 60

Slide 60 text

fun order(f: Order.() -> Unit): Order.() -> Unit = f fun buy(f: Buy.() -> Unit)= {} Demo

Slide 61

Slide 61 text

References https://kotlinlang.org/docs/reference/type-safe-builders.html https://github.com/agoda-com/Kakao https://github.com/gradle/kotlin-dsl

Slide 62

Slide 62 text

@inyaki_mwc Thanks