|
1 | | -# About |
| 1 | +# About Basics |
2 | 2 |
|
3 | | -## Type inference |
| 3 | +Kotlin is a statically typed language, designed to be fully interoperable with Java. |
4 | 4 |
|
5 | | -Kotlin compiler can infer types for functions and variables in most of the cases. However, declaring function return types explicitly for **public API** is a good practice. |
| 5 | +Distinguishing it from Java, Kotlin: |
6 | 6 |
|
7 | | -## Entry point |
| 7 | +- has a cleaner, more concise syntax; |
| 8 | +- incorporates many features of functional languages; |
| 9 | +- has extensive support for nullable values. |
8 | 10 |
|
9 | | -To run your Kotlin program you need to define so-called entry point: top-level function with name `main` with or without arguments. However, in Exercism course we are using automatic tests that are using other functions defined in exercises. |
| 11 | +## Variables |
10 | 12 |
|
11 | | -Entry point with arguments (you can use them for building CLI applications): |
| 13 | +Because Kotlin is statically typed, it is necessary to _know_ the type of each value at compile time. |
| 14 | + |
| 15 | +However, Kotlin's [type inference][inference] is very powerful, so _specifying_ the type is often optional. |
| 16 | + |
| 17 | +There are two ways to declare a variable. |
| 18 | + |
| 19 | +1. `val` creates an immutable variable, and trying to change it is a compile-time error. |
12 | 20 |
|
13 | 21 | ```kotlin |
14 | | -fun main(args: Array<String>) { |
15 | | - println("Hello, Exercism!") |
16 | | - println("Program args are: $args") |
17 | | -} |
| 22 | +val x = 42 // => 42 |
| 23 | +x = 43 // => 'val' cannot be reasssigned |
| 24 | +``` |
| 25 | + |
| 26 | +2. `var` creates a mutable variable. |
| 27 | + |
| 28 | +```Kotlin |
| 29 | +var x = 42 // => 42 |
| 30 | +x = 43 // => 43 |
18 | 31 | ``` |
19 | 32 |
|
20 | | -Or ignore arguments completely: |
| 33 | +Because immutable variables eliminate a common class of bugs, use of `val` is encouraged whenever possible. |
| 34 | + |
| 35 | +Also, even a `var` cannot change type: |
21 | 36 |
|
22 | 37 | ```kotlin |
23 | | -fun main() { |
24 | | - println("Hello, Exercism!") |
| 38 | +var x = 42 |
| 39 | +x = "foobar" // => Type mismatch: inferred type is String but Int was expected |
| 40 | +``` |
| 41 | + |
| 42 | +To reduce visual distraction, explicit types will mostly be omitted from this syllabus. |
| 43 | +Nevertheless, it is recommended to specify types at least for: |
| 44 | + |
| 45 | +- public APIs |
| 46 | +- function signatures |
| 47 | +- where it needs documentation |
| 48 | + |
| 49 | +Some companies and organisations will require strict type safety and disallow type inference, whilst others restrict it to "simple" types, or embrace type inference fully. |
| 50 | + |
| 51 | +```kotlin |
| 52 | +val x: Int = 42 // => 42 |
| 53 | +``` |
| 54 | + |
| 55 | +In general, the names of variables and functions should be in `camelCase`, not `snake_case` |
| 56 | + |
| 57 | +## Functions |
| 58 | + |
| 59 | +Declare a function with the `fun` keyword. |
| 60 | +Unlike Java, functions are not required to be part of a class. |
| 61 | + |
| 62 | +```Kotlin |
| 63 | +fun hello(): String { |
| 64 | + return "Hello, World!" |
| 65 | +} |
| 66 | + |
| 67 | +fun add(x: Int, y: Int): Int { |
| 68 | + return x + y |
25 | 69 | } |
26 | 70 | ``` |
27 | 71 |
|
28 | | -## `Unit` return type |
| 72 | +Some points to note: |
| 73 | + |
| 74 | +- Parentheses `()` are needed after the function name, even if the function takes no arguments. |
| 75 | +- Function arguments need to specify the type: there is no type inference (in contrast to variables). |
| 76 | +- The body of the function is enclosed in braces `{ }` (though see below). |
| 77 | +- The `return` keyword is required, if returning a value. |
| 78 | +- Semicolons `;` at the end of lines are optional, and usually omitted. |
| 79 | +- Arithmetic operators `+`, `-`, `*`, `/` are similar to most mainstream languages. |
29 | 80 |
|
30 | | -You've seen that some functions (like `main()` above) are not returning value. However, they are implicitly returning value that is called `Unit` (quite similar to `void` in Java/C/C++): |
| 81 | +However, for these very simple, "single-expression" functions, there is an abbreviated syntax: |
| 82 | + |
| 83 | +```Kotlin |
| 84 | +// return type is usually omitted for single-expression functions |
| 85 | +fun add(x: Int, y: Int) = x + y |
| 86 | +``` |
| 87 | +Functions can have parameters with default values. |
| 88 | +These values will be used if they are omitted where the function is invoked: |
31 | 89 |
|
32 | 90 | ```kotlin |
33 | | -fun run() {} |
| 91 | +fun ping(host: String = "localhost") { |
| 92 | + println("PING --> $host") |
| 93 | +} |
| 94 | + |
| 95 | +ping("exercism.io") // PING --> exercism.io |
| 96 | +ping() // PING --> localhost |
| 97 | +``` |
| 98 | + |
| 99 | +Functions within Exercism will usually return a value (because of the way the test runner is structured). |
| 100 | +To use Kotlin more widely, it may be useful to know that a function which returns no value can omit the return type and the return keyword. |
34 | 101 |
|
35 | | -// is the same as |
36 | | -fun run(): Unit { return Unit } |
| 102 | +It is then said to have a `Unit` return type: equivalent to `void` in Java and several other languages. |
| 103 | + |
| 104 | +## Comments |
| 105 | + |
| 106 | +Single-line comments start with `//`, and the rest of the line is then ignored. |
| 107 | + |
| 108 | +Multi-line comments start with `/*` and end with `*/` |
| 109 | + |
| 110 | +```Kotlin |
| 111 | +fun hello(): String { |
| 112 | + /* |
| 113 | + * Failing to run this program on a new installation |
| 114 | + * is considered bad luck |
| 115 | + */ |
| 116 | + return "Hello, World!" // the compiler pixies are now happy |
| 117 | +} |
37 | 118 | ``` |
38 | 119 |
|
39 | | -Returning `Unit` (and using it in function declaration) is completely optional and is omitted in most of the cases. |
| 120 | +This is the same as Java. |
| 121 | + |
| 122 | +~~~~exercism/note |
| 123 | +Because of the importance of Java interop, many Kotlin learners are at least somewhat familiar with Java. |
| 124 | +
|
| 125 | +We will try to point out similarities and differences between the languages throughout the syllabus. |
| 126 | +
|
| 127 | +_Please ignore this if you are a Kotlin-first learner!_ |
| 128 | +~~~~ |
| 129 | + |
40 | 130 |
|
41 | | -[intellij-idea-ic]: https://www.jetbrains.com/idea/download/ |
| 131 | +[inference]: https://en.wikipedia.org/wiki/Type_inference |
0 commit comments