|
1 |
| -* https://kotlinlang.org/docs/reference/scope-functions.html |
2 |
| -* https://www.manning.com/books/kotlin-in-action |
3 |
| - |
4 | 1 | # kotlin-dsl-lambda-workshop
|
5 | 2 |
|
| 3 | +* reference |
| 4 | + * https://kotlinlang.org/docs/reference/scope-functions.html |
| 5 | + * https://www.manning.com/books/kotlin-in-action |
| 6 | + |
6 | 7 | ## extension function
|
7 |
| -* Adding methods to other people’s classes: |
8 |
| - extension functions and properties |
9 |
| - * Conceptually, an extension function is a simple thing: it’s a function that can be |
10 |
| - called as a member of a class but is defined outside of it |
11 |
| - * package strings |
12 |
| - fun String.lastChar(): Char = this.get(this.length - 1) |
13 |
| - * The receiver type (String) is the type on which the extension is |
14 |
| - defined, and the receiver object (this) is the instance of that type |
15 |
| - * as in a regular method, you can omit this: fun String.lastChar(): Char = get(length - 1) |
16 |
| - |
17 | 8 | * top-level function
|
18 |
| - * when you compile the file, some classes will be pro- |
19 |
| - duced, because the JVM can only execute code in classes |
20 |
| - * you can |
21 |
| - place functions directly at the top level of a source file, outside of any class |
| 9 | + * JVM can only execute code in classes |
| 10 | + * you can place functions directly at the top level of a source file, outside of any class |
| 11 | + * when you compile the file, some classes will be produced |
| 12 | + ``` |
22 | 13 | package strings // filename: join.kt
|
23 | 14 | fun joinToString(...): String { ... }
|
24 | 15 |
|
|
27 | 18 | public class JoinKt { // Corresponds to join.kt, the filename
|
28 | 19 | public static String joinToString(...) { ... }
|
29 | 20 | }
|
| 21 | + ``` |
| 22 | +* it’s a function that can be called as a member of a class but is defined outside of it |
| 23 | +* example |
| 24 | + ``` |
| 25 | + fun String.lastChar(): Char = this.get(this.length - 1) |
| 26 | + fun String.lastChar(): Char = get(length - 1) // this is implicit |
| 27 | + ``` |
| 28 | + * receiver type: String - type on which the extension is defined |
| 29 | + * receiver object: this - the instance of that type |
30 | 30 |
|
31 | 31 | ## lambdas with receivers
|
32 | 32 | * the ability to call methods of a different object in the body of a
|
|
0 commit comments