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

Kotlin DSL

Kotlin DSL

Vinh Nguyen

October 19, 2018
Tweet

More Decks by Vinh Nguyen

Other Decks in Programming

Transcript

  1. More Kotlin features • Kotlin first-class function • OOP delegation

    & object receiver in Kotlin • inline function
  2. Kotlin first-class function –Kotlinlang “Kotlin functions are first-class, which means

    that they can be stored in variables and data structures, passed as arguments to and returned from other higher-order functions. You can operate with functions in any way that is possible for other non- function values.”
  3. Higher-order function –Kotlinlang “A higher-order function is a function that

    takes functions as parameters, or returns a function.”
  4. Higher-order function class Benchmark { fun benchmark(block: () -> Unit):

    Long { val startTime = System.currentTimeMillis() block.invoke() return System.currentTimeMillis() - startTime } }
  5. Function types • All function types have a parenthesized parameter

    types list and a return type: (A, B) -> C denotes a type that represents functions taking two arguments of types A and B and returning a value of type C. • The parameter types list may be empty, as in () -> A. • Function types can optionally have an additional receiver type A.(B) -> C, which represents functions that can be called on a receiver object of A with a parameter of B and return a value of C. • Function type is just a syntactic sugar for an interface, but the interface cannot be used explicitly. Nevertheless we can use function types like interfaces, what includes using them as type arguments or implementing them.
  6. Function types val greet: () -> Unit val calculateTwoInt: (Int,

    Int) -> Int val square: (Int) -> Int val producePrinter: () -> () -> Unit val sum: (Int, Int) -> Int = { a, b -> a + b } class MyFunction : () -> Unit { override fun invoke() { println("I am called") } } fun main(args: Array<String>) { val function = MyFunction() println(sum(1, 2)) // Prints: 3 function() // Prints: I am called }
  7. Function reference • Function reference is referencing to the actual

    function. • The simplest way to provide function
  8. Function reference val sumTwoInt: (Int, Int) -> Int = {

    a, b -> a + b } fun greetFunction() { println("Hello") } val reference1 = ::sumTwoInt val reference2 = sumTwoInt val reference3 = ::greetFunction fun anotherSumTwoInt() = sumTwoInt fun anotherGreatFunction() = ::greetFunction
  9. Function literal • Literal in programming is a syntactic sugar

    for representing values of some types the language considers particularly important. • Function literal is a special notation used to simplify how a function is defined. • Two types of function literals: Lambda expression & Anonymous function.
  10. Lambda expression • Lambda expression is a short way to

    define a function. val greet: () -> Unit = { println("Hello") } val calculateTwoInt: (Int, Int) -> Int = { a, b -> a + b } val square: (Int) -> Int = { x -> x * x } val producePrinter: () -> () -> Unit = { { println("I am printing") } } val greet = { println("Hello") } val calculateTwoInt = { a, b -> a + b } val square = { x: Int -> x * x } val producePrinter = { { println("I am printing") } } greet() // Prints: Hello calculateTwoInt(2, 3) // Prints: 5 println(square(2)) // Prints: 4 producePrinter()() // Prints: I am printing
  11. Anonymous function • Anonymous function is an alternative way to

    define a function. val greet: () -> Unit = fun() { println("Hello") } val calculateTwoInt: (Int, Int) -> Int = fun(a, b): Int { return a + b } val calculateTwoInt: (Int, Int) -> Int = fun(a, b) = a + b val square: (Int) -> Int = fun(x) = x * x val producePrinter: () -> () -> Unit = fun() = fun() { println("I am printing") } val greet = fun() { println("Hello") } val calculateTwoInt = fun(a: Int, b: Int): Int = a + b val square = fun(x: Int) = x * x val producePrinter = fun() = fun() { println("I am printing") }
  12. Lambda Exp vs. Anon Function val getMessage = { response:

    Response -> if(response.code !in 200..299) { return "Error" // Error! Not allowed } response.message } val getMessage = lambda@ { response: Response -> if(response.code !in 200..299) { return@lambda “Error" // Return at labels } response.message } val getMessage = fun(response: Response): String { if(response.code !in 200..299) { return "Error" } return response.message }
  13. OOP delegation –Wikipedia “In object-oriented programming, delegation refers to evaluating

    a member (property or method) of one object (the receiver) in the context of another original object (the sender).”
  14. OOP delegation class Car { private val engine = Engine()

    fun startEngine() { engine.run() } }
  15. Explicit & implicit receiver • Receiver is explicit if it

    refers to an object when accessing its members. • Receiver is implicit if it don’t explicitly refer to object when accessing its members.
  16. Explicit & implicit receiver val guitar = Guitar() guitar.playTone() //

    explicit receiver class Guitar { fun playTone() { } fun playSong() { this.playTone() // explicit receiver playTone() // implicit receiver } }
  17. Explicit & implicit receiver interface A { fun doSomething() {

    println("A is doing something") } } interface B { fun doSomething() { println("B is doing something") } } class Test : A, B { override fun doSomething() { doSomething() // infinitely recursive call } } class Test : A, B { override fun doSomething() { super<A>.doSomething() // explicit receiver super<B>.doSomething() // explicit receiver } }
  18. Extension & dispatch receiver • Extension receiver is the receiver

    that is closely related to Kotlin extensions. Extension receiver represents an object that we define an extension for. • Dispatch receiver is a special kind of receiver existing when the extension is declared a member. It represents an instance of the class in which the extension is declared in.
  19. Extension & dispatch receiver class Ball(var name: String) fun Ball.bounce()

    { println("Receiver type is ${this.javaClass}, " + "Receiver object is ${this}") } val ball = Ball("Golf ball") ball.bounce()
  20. Extension & dispatch receiver class Person { fun move() {}

    } class NetworkRepository(val person: Person) { fun loadData() {} fun move() {} fun doSomething() { person.uploadToBackend(); // We can access extension here } fun Person.uploadToBackend() { //method from extension dispatch receiver loadData() //method from extension receiver // calls method defined in Person class move() // calls method defined in NetworkRepository class this@NetworkRepository.move() } } val person = Person() person.uploadToBackend() // Compilation error
  21. This Expression • To denote the current receiver, we use

    this expressions: • In a member of a class, this refers to the current object of that class. • In an extension function or a function literal with receiver this denotes the receiver parameter that is passed on the left-hand side of a dot. • If this has no qualifiers, it refers to the innermost enclosing scope. To refer to this in other scopes, label qualifiers are used => this@label
  22. This Expression class A { // implicit label @A inner

    class B { // implicit label @B fun Int.foo() { // implicit label @foo val a = this@A // A's this val b = this@B // B's this val c = this // foo()'s receiver, an Int val c1 = this@foo // foo()'s receiver, an Int val funLit = fun String.() { val d = this // funLit's receiver } val funLit2 = { s: String -> // foo()'s receiver, since enclosing lambda expression // doesn't have any receiver val d1 = this } } } }
  23. Function type with receiver & Function literal with receiver •

    Function types can optionally have an additional receiver type A.(B) -> C, which represents functions that can be called on a receiver object of A with a parameter of B and return a value of C. class Person(var abc: String) val addNickName: Person.(nickName: String) -> String = { nickName -> this.abc + " is " + nickName } fun main(arg: Array<String>) { println(addNickName(Person("Vinh"), "Vince")) }
  24. Function type with receiver & Function literal with receiver val

    squareWithoutReceiver: (Int) -> Int = { num -> num * num } val squareWithoutReceiver1: (Int) -> Int = { it * it } val squareWithReceiver: Int.() -> Int = { this * this } val squareWithReceiverFunc: Int.() -> Int = fun Int.() = this * this val squareWithReceiverFunc1 = fun Int.() = this * this val squareFunc = { this, this } // compile error
  25. run, with, apply, also, let fun <T, R> T.run(block: T.()

    -> R): R = block() fun <T, R> with(receiver: T, block: T.() -> R): R = receiver.block() fun <T> T.apply(block: T.() -> Unit): T { block(); return this } fun <T> T.also(block: (T) -> Unit): T { block(this); return this } fun <T, R> T.let(block: (T) -> R): R = block(this) https://docs.google.com/spreadsheets/d/1P2gMRuu36pSDW4fdwE- fLN9fcA_ZboIU2Q5VtgixBNo/edit#gid=0
  26. apply() example inline fun <T> T.apply(block: T.() -> Unit): T

    { block(this) return this } val editor = preferences.edit() editor.apply { putString("key_String", "value") putInt("key_int", 0) apply() }
  27. Inline function • Using higher-order functions imposes certain runtime penalties:

    each function is an object, and it captures a closure, i.e. those variables that are accessed in the body of the function. Memory allocations (both for function objects and classes) and virtual calls introduce runtime overhead. • The inline modifier affects both the function itself and the lambdas passed to it: all of those will be inlined into the call site.
  28. Inline function fun nonInlined(block: () -> Unit) { println("before") block()

    println("after") } public void nonInlined(Function block) { System.out.println("before"); block.invoke(); System.out.println("after"); } nonInlined { println("do something here") } nonInlined(new Function() { @Override public void invoke() { System.out.println("do something here"); } });
  29. Inline function inline fun inlined(block: () -> Unit) { println("before")

    block() println("after") } inlined { println("do something here") } System.out.println(“before"); System.out.println("do something here”); System.out.println("after");
  30. Noninline function inline fun higherOrderFunction( aLambda: () -> Unit, noinline

    dontInlineLambda: () -> Unit, aLambda2: () -> Unit ) { doSomething() aLambda() dontInlineLambda()//won't be inlined. aLambda2() doAnotherThing() }
  31. Return at labels • With function literals, local functions and

    object expression, functions can be nested in Kotlin. Qualified returns allow us to return from an outer function. The most important use case is returning from a lambda expression. fun foo() { listOf(1, 2, 3, 4, 5).forEach { // non-local return directly to the caller of foo() if (it == 3) return print(it) } println("this point is unreachable") }
  32. Return at labels fun foo() { listOf(1, 2, 3, 4,

    5).forEach lit@ { // local return to the caller of the lambda, i.e. the forEach loop if (it == 3) return@lit print(it) } print("done with explicit label") } val getMessage = lambda@ { response: Response -> if(response.code !in 200..299) { return@lambda “Error" // Return at labels } response.message }
  33. Crossinline • The crossinline marker is used to mark lambdas

    that mustn’t allow non-local returns, especially when such lambda is passed to another execution context such as a higher order function that is not inlined, a local object or a nested function. inline fun higherOrderFunction(crossinline lambda: () -> Unit) { normalFunction { lambda() } } fun normalFunction(func: () -> Unit) { return } fun callingFunction() { higherOrderFunction { return //Error. Can't return from here. } }
  34. DSL introduction –Wikipedia 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.
  35. DSL introduction • DSL allows to write not imperative code

    (way how to solve the problem) but in more or less declarative way (just declare the task) in order to obtain the solution based on the given data. • External DSLs have their own custom syntax, have to write a full parser to process them. • Internal DSLs are particular ways of using a host language to give the host language the feel of a particular language.
  36. DSL example • External DSL: CSS, HTML, SQL, Regex,… •

    Internal DSL • Java: jOOQ, AssertJ, Hamcrest • Gradle: build.gradle in Android • Kotlin: ???
  37. Build DSL in Kotlin val person = person { name

    = "John" age = 25 address { street = "Main Street" number = 42 city = "London" } } data class Person( var name: String? = null, var age: Int? = null, var address: Address? = null ) data class Address( var street: String? = null, var number: Int? = null, var city: String? = null )
  38. Build DSL in Kotlin Person( name = "John", age =

    25, address = Address( street = "Main Street", number = 42, city = “London" ) ) val person = person { name = "John" age = 25 address { street = "Main Street" number = 42 city = "London" } }
  39. Build DSL in Kotlin fun person(block: Person.() -> Unit): Person

    { val p = Person() p.block() return p } fun Person.address(block: Address.() -> Unit) { address = Address().apply(block) } val person = person { name = "John" age = 25 address { street = "Main Street" number = 42 city = "London" } }
  40. Builder Pattern data class Person( val name: String, val dateOfBirth:

    Date, var address: Address? ) data class Address( val street: String, val number: Int, val city: String ) val person = person { name = "John" dateOfBirth = "1980-12-01" address { street = "Main Street" number = 12 city = "London" } }
  41. Builder Pattern class PersonBuilder { var name: String = ""

    private var dob: Date = Date() var dateOfBirth: String = "" set(value) { dob = SimpleDateFormat("yyyy-MM-dd").parse(value) } private var address: Address? = null fun address(block: AddressBuilder.() -> Unit) { address = AddressBuilder().apply(block).build() } fun build(): Person = Person(name, dob, address) } class AddressBuilder { var street: String = “" var number: Int = 0 var city: String = "" fun build() : Address = Address(street, number, city) } fun person(block: PersonBuilder.() -> Unit): Person = PersonBuilder().apply(block).build()
  42. Collections data class Person( val name: String, val dateOfBirth: Date,

    val addresses: List<Address> ) data class Address( val street: String, val number: Int, val city: String ) val person = person { name = "John" dateOfBirth = "1980-12-01" addresses { address { street = "Main Street" number = 12 city = "London" } address { street = "Dev Avenue" number = 42 city = "Paris" } } }
  43. Collections class PersonBuilder { // ... other properties private val

    addresses = mutableListOf<Address>() fun address(block: AddressBuilder.() -> Unit) { addresses.add(AddressBuilder().apply(block).build()) } fun build(): Person = Person(name, dob, addresses) } val person = person { name = "John" dateOfBirth = "1980-12-01" address { street = "Main Street" number = 12 city = "London" } address { street = "Dev Avenue" number = 42 city = "Paris" } }
  44. Collections (Improvement) class PersonBuilder { // ... other properties private

    val addresses = mutableListOf<Address>() fun addresses(block: ADDRESSES.() -> Unit) { addresses.addAll(ADDRESSES().apply(block)) } fun build(): Person = Person(name, dob, addresses) } class ADDRESSES: ArrayList<Address>() { fun address(block: AddressBuilder.() -> Unit) { add(AddressBuilder().apply(block).build()) } } val person = person { name = "John" dateOfBirth = "1980-12-01" addresses { address { street = "Main Street" number = 12 city = "London" } address { street = "Dev Avenue" number = 42 city = "Paris" } } }
  45. Narrowing scope val person = person { name = "John"

    dateOfBirth = "1980-12-01" addresses { address { addresses { name = "Mary" // access outer scope lambda } street = "Dev Avenue" number = 42 city = "Paris" } } }
  46. Narrowing scope @DslMarker annotation class PersonDsl @PersonDsl class PersonBuilder {

    //... } @PersonDsl class ADDRESSES: ArrayList<Address>() { //... } @PersonDsl class AddressBuilder { //... } addresses { name = “Mary” // error } this@person.name = “Mary” https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-dsl-marker/index.html @DslMarker
  47. Build DSL in Kotlin (Android) inline fun Context.linearLayout(block: LinearLayout.() ->

    Unit) = LinearLayout(this).apply(block) inline fun ViewGroup.view(block: View.() -> Unit) = View(context).apply(block) val layout = linearLayout { orientation = LinearLayout.HORIZONTAL addView(view { layoutParams.width = 100 layoutParams.height = 40 }) }
  48. Anko • Anko is a Kotlin library which makes Android

    application development faster and easier. It makes your code clean and easy to read, and lets you forget about rough edges of the Android SDK for Java. • Anko supports: Andorid components (Intent, dialog, logging,…), UI layouts, SQLite, Coroutines • https://github.com/Kotlin/anko
  49. Anko layout override fun createView(ui: AnkoContext<MainActivity>) = with(ui) { verticalLayout

    { padding = dip(32) imageView(android.R.drawable.ic_menu_manage).lparams { margin = dip(16) gravity = Gravity.CENTER } val name = editText { hintResource = R.string.name } val password = editText { hintResource = R.string.password inputType = TYPE_CLASS_TEXT or TYPE_TEXT_VARIATION_PASSWORD } button("Log in") { onClick { ui.owner.tryLogin(ui, name.text, password.text) } } myRichView() }.applyRecursively(customStyle) }
  50. Anko layout strength • Type-safe • Support tree structure •

    Simplicity • Easier to control UI elements in the code • Code reusability
  51. Koin • KOIN - a pragmatic lightweight dependency injection framework

    for Kotlin • Written in pure Kotlin, using functional resolution only: no proxy, no code generation, no reflection. • https://insert-koin.io
  52. Koin example class Controller(val service : BusinessService) class BusinessService() val

    myModule = module { single { Controller(get()) } single { BusinessService() } } class MyApplication : Application() { override fun onCreate(){ super.onCreate() startKoin(this, listOf(myModule)) } } class MyActivity() : AppCompatActivity() { val service : BusinessService by inject() override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) val service : BusinessService = get() } }
  53. Koin strength • No proxy, no code generation, no reflection

    • Lazy injection • Easy to learn • Simple to write
  54. Reference • https://blog.kotlin-academy.com/ • https://kotlinlang.org/docs/reference/lambdas.html • https://kotlinlang.org/docs/reference/inline-functions.html • https://docs.google.com/presentation/d/ 12mkyGQZO22kf0_6kp2K6xyFdpg0nBLqGtNcVR-cV4M8/pub?slide=id.p

    • https://www.martinfowler.com/bliki/DomainSpecificLanguage.html • https://proandroiddev.com/writing-dsls-in-kotlin-part-1-7f5d2193f277 • https://proandroiddev.com/writing-dsls-in-kotlin-part-2-cd9dcd0c4715 • https://github.com/InsertKoinIO/koin • https://github.com/Kotlin/anko
  55. Q&A