Slide 1

Slide 1 text

Function Literals with Receiver Ryan Casler @ryanrampage1 github.com/ryanrampage1

Slide 2

Slide 2 text

Terms

Slide 3

Slide 3 text

Function Literal A function that is not declared, but passed immediately as an expression

Slide 4

Slide 4 text

Lambda A type of function literal using curly braces EX. val sum = { x: Int, y: Int -> x + y }

Slide 5

Slide 5 text

Receiver A class that is gaining a function

Slide 6

Slide 6 text

Fun Stuff

Slide 7

Slide 7 text

What Are They ● A function ● Can be passed into other functions ● Can be a Lambda ● Has access to the receivers members

Slide 8

Slide 8 text

Call sites can pass in a new function for any existing class

Slide 9

Slide 9 text

Receiver.(SomeArg) -> Unit

Slide 10

Slide 10 text

How to Use

Slide 11

Slide 11 text

Examples

Slide 12

Slide 12 text

Standard Library Apply

Slide 13

Slide 13 text

Standard Library With

Slide 14

Slide 14 text

Standard Library Run

Slide 15

Slide 15 text

Batch Light

Slide 16

Slide 16 text

Batch SQLite Insert Steps 1. Chunk the list 2. Create strings to represent SQL 3. Compile SQLite statements 4. Bind items to the statement 5. Execute the statement

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

Binding Items 1. Bind each item 2. Track the current Bind Index 3. Use the correct typed bind function for each value a. bindString, bindLong ect

Slide 19

Slide 19 text

Library Implementation

Slide 20

Slide 20 text

Call Site

Slide 21

Slide 21 text

Type Safe Builder

Slide 22

Slide 22 text

Kotlin HTML Builder

Slide 23

Slide 23 text

HTML Builder Implementation

Slide 24

Slide 24 text

Domain-Specific Language (DSL)

Slide 25

Slide 25 text

Sources ● ● ● ●