Fun with DSL in Kotlin

Sponsored · Ship Features Fearlessly Turn features on and off without deploys. Used by thousands of Ruby developers. →

Fun with DSL in Kotlin

Droidcon Singapore 2018

Iñaki Villar

April 05, 2018

Annual Pro sale · Save $30

Have presentations of your own?

Share with private links, password protection, and more control over your work.

Transcript

62 slides
  1. DSL

  2. https://github.com/kotlintest public infix fun <T> T.should(matcher: (T) -> kotlin.Unit): kotlin.Unit

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

    { } public infix fun <T, U : T> T.shouldBe(any: U?): kotlin.Unit { } public infix fun <T> T.shouldHave(matcher: io.kotlintest.Matcher<T>): kotlin.Unit { } public infix fun <T> T.shouldNot(matcher: io.kotlintest.Matcher<T>): kotlin.Unit { } public infix fun <T> 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) } } } }
  4. data class Salary(private val quantity: Double) { infix fun taxes(amount:

    Double): Salary { return Salary(quantity - amount) } }
  5. 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 }
  6. lambdas expressions val sum: (Int, Int) -> Int = {

    x, y -> x + y } val sum = { x: Int, y: Int -> x + y }
  7. 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)
  8. 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) })
  9. 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) })
  10. 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) }
  11. return with(Salary()) { gross = 1000.00 taxes = 200.00 pension

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

    = 100.00 calculate() } lambda with receiver fun <T, R> with(receiver: T, block: T.() -> R): R = receiver.block()
  13. fun salary(f: Salary.() -> Unit): Salary { val salary =

    Salary() salary.f() return salary } class Salary { fun calculate() {} } salary { calculate() }
  14. class Order(val buy: Buy) class Buy(var price: Double, var stopLoss:

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

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

    Double, var marginProfit: Double) fun order(f: Order.() -> Unit): Order.() -> Unit = f
  17. fun order(f: Order.() -> Unit): Order.() -> Unit = f

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

More Decks by Iñaki Villar

Other Decks in Technology