|
5 | 5 | * reference
|
6 | 6 | * https://kotlinlang.org/docs/reference/scope-functions.html
|
7 | 7 | * https://www.manning.com/books/kotlin-in-action
|
| 8 | + * https://stackoverflow.com/questions/33190613/what-does-a-kotlin-function-signature-with-t-mean |
8 | 9 | * please refer previously: https://github.com/mtumilowicz/groovy258-dsl-closure-workshop
|
9 | 10 | * this workshop is analogous but in kotlin
|
10 | 11 |
|
|
49 | 50 |
|
50 | 51 | ## lambdas with receivers
|
51 | 52 | * the ability to call methods of a different object in the body of a lambda
|
52 |
| -* example (from standard library) |
| 53 | +* example (from standard library - look for others) |
53 | 54 | * `apply` - calls the specified function `block` with `this` value as its receiver and returns
|
54 | 55 | `this` value
|
55 | 56 | ```
|
|
58 | 59 | contract {
|
59 | 60 | callsInPlace(block, InvocationKind.EXACTLY_ONCE)
|
60 | 61 | }
|
61 |
| - block() |
| 62 | + block() // this.block() |
62 | 63 | return this
|
63 | 64 | }
|
64 | 65 | ```
|
|
72 | 73 | }
|
73 | 74 | println(name) // Michal
|
74 | 75 | ```
|
75 |
| - * `T.run(block: T.() -> R)` |
76 |
| - * calls the specified function `block` with `this` value as its receiver and returns its result |
77 |
| - * `with(receiver: T, block: T.() -> R)` |
78 |
| - * calls the specified function `block` with `receiver` as its receiver and returns its result |
79 | 76 | * lambdas with receiver and extension functions
|
80 | 77 | * note that an extension function is, in a sense, a function with a receiver
|
81 | 78 | * `this` refers to the instance of the type the function is extending
|
|
105 | 102 | ```
|
106 | 103 | sign("Approved by: ", "Michal Tumilowicz")
|
107 | 104 | ```
|
| 105 | + * what is exactly `T.()`? |
| 106 | + * `fun doSomethingWithInt(receiver: Int.() -> Unit)` |
| 107 | + * how this is different from Regular lambda like `() -> Unit`? |
| 108 | + * similar to extension function we could call: `5.receiver()` |
108 | 109 | * therefore, a lambda with a receiver looks exactly the same as a regular lambda in the source code
|
109 | 110 |
|
110 | 111 | ## infix
|
|
0 commit comments