Skip to content

Commit 18bac21

Browse files
authored
Merge pull request #4576 from JetBrains/2-1-0-doc-update
feat: add Kotlin 2.1.0 release details
2 parents bee846f + 4d0b2f7 commit 18bac21

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+2873
-540
lines changed

data/releases.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
url: https://github.com/JetBrains/kotlin/releases
22

33
latest:
4-
version: 2.0.21
5-
url: https://github.com/JetBrains/kotlin/releases/tag/v2.0.21
4+
version: 2.1.0
5+
url: https://github.com/JetBrains/kotlin/releases/tag/v2.1.0
-124 KB
Binary file not shown.

docs/kr.tree

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@
2323
<toc-element topic="competitive-programming.md"/>
2424
</toc-element>
2525
<toc-element toc-title="What's new in Kotlin">
26-
<toc-element toc-title="Kotlin 2.0.20" accepts-web-file-names="whatsnew.html" topic="whatsnew2020.md"/>
27-
<toc-element toc-title="Kotlin 2.0.0" topic="whatsnew20.md"/>
28-
<toc-element toc-title="Kotlin 2.1.0-RC2" topic="whatsnew-eap.md"/>
26+
<toc-element toc-title="Kotlin 2.1.0" accepts-web-file-names="whatsnew.html" topic="whatsnew21.md"/>
27+
<toc-element toc-title="Kotlin 2.1.0-RC2" topic="whatsnew-eap.md" hidden="true"/>
2928
<toc-element toc-title="Earlier versions">
29+
<toc-element toc-title="Kotlin 2.0.20" topic="whatsnew2020.md"/>
30+
<toc-element toc-title="Kotlin 2.0.0" topic="whatsnew20.md"/>
3031
<toc-element toc-title="Kotlin 1.9.20" topic="whatsnew1920.md"/>
3132
<toc-element toc-title="Kotlin 1.9.0" topic="whatsnew19.md"/>
3233
<toc-element toc-title="Kotlin 1.8.20" topic="whatsnew1820.md"/>
@@ -119,9 +120,8 @@
119120
</toc-element>
120121
<toc-element toc-title="Multiplatform development">
121122
<toc-element accepts-web-file-names="building-mpp-with-gradle.html,intro-to-kotlin-mpp.html,mpp-intro.html,mpp-get-started.html,multiplatform-tutorials.html,multiplatform-get-started.html" toc-title="Introduction" topic="multiplatform-intro.md"/>
122-
<toc-element accepts-web-file-names="mpp-discover-project.html" toc-title="Understand basic project structure" topic="multiplatform-discover-project.md"/>
123+
<toc-element accepts-web-file-names="mpp-discover-project.html,mpp-set-up-targets.html,multiplatform-set-up-targets.html" toc-title="Understand basic project structure" topic="multiplatform-discover-project.md"/>
123124
<toc-element toc-title="Explore advanced project structure" topic="multiplatform-advanced-project-structure.md"/>
124-
<toc-element toc-title="Set up targets" accepts-web-file-names="mpp-set-up-targets.html" topic="multiplatform-set-up-targets.md"/>
125125
<toc-element toc-title="Share code">
126126
<toc-element accepts-web-file-names="mpp-share-on-platforms.html" topic="multiplatform-share-on-platforms.md"/>
127127
<toc-element topic="multiplatform-expect-actual.md"/>

docs/topics/coding-conventions.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -980,7 +980,7 @@ For example, use this syntax with `if`:
980980
if (x == null) ... else ...
981981
```
982982

983-
instead of this one with `when`:
983+
Instead of this one with `when`:
984984

985985
```kotlin
986986
when (x) {
@@ -991,6 +991,24 @@ when (x) {
991991

992992
Prefer using `when` if there are three or more options.
993993

994+
### Guard conditions in when expression
995+
996+
Use parentheses when combining multiple boolean expressions in `when` expressions or statements with [guard conditions](control-flow.md#guard-conditions-in-when-expressions):
997+
998+
```kotlin
999+
when (status) {
1000+
is Status.Ok if (status.info.isEmpty() || status.info.id == null) -> "no information"
1001+
}
1002+
```
1003+
1004+
Instead of:
1005+
1006+
```kotlin
1007+
when (status) {
1008+
is Status.Ok if status.info.isEmpty() || status.info.id == null -> "no information"
1009+
}
1010+
```
1011+
9941012
### Nullable Boolean values in conditions
9951013

9961014
If you need to use a nullable `Boolean` in a conditional statement, use `if (value == true)` or `if (value == false)` checks.

docs/topics/compiler-reference.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ Suppress the compiler from displaying warnings during compilation.
5454
5555
Turn any warnings into a compilation error.
5656
57+
### -Wextra
58+
59+
Enable [additional declaration, expression, and type compiler checks](whatsnew21.md#extra-compiler-checks) that
60+
emit warnings if true.
61+
5762
### -verbose
5863
5964
Enable verbose logging output which includes details of the compilation process.
@@ -131,6 +136,14 @@ $ kotlinc @options/compiler.options hello.kt
131136
Enable usages of API that [requires opt-in](opt-in-requirements.md) with a requirement annotation with the given
132137
fully qualified name.
133138

139+
### -Xsuppress-warning
140+
141+
Suppresses specific warnings [globally across the whole project](whatsnew-eap.md), for example:
142+
143+
```bash
144+
kotlinc -Xsuppress-warning=NOTHING_TO_INLINE -Xsuppress-warning=NO_TAIL_CALLS_FOUND main.kt
145+
```
146+
134147
## Kotlin/JVM compiler options
135148

136149
The Kotlin compiler for JVM compiles Kotlin source files into Java class files.

docs/topics/compose-compiler-options.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,10 @@ This results in fewer slots being used and fewer comparisons being made at runti
181181

182182
### OptimizeNonSkippingGroups
183183

184+
> This feature is considered [Experimental](components-stability.md#stability-levels-explained).
185+
>
186+
{style="warning"}
187+
184188
**Default**: disabled
185189

186190
If enabled, remove groups around non-skipping composable functions.
@@ -190,10 +194,27 @@ unnecessary groups around composables which do not skip (and thus do not require
190194
This optimization will remove the groups, for example, around functions explicitly marked as `@NonSkippableComposable`
191195
and functions that are implicitly not skippable (inline functions and functions that return a non-`Unit` value such as `remember`).
192196

193-
> This feature is considered [Experimental](components-stability.md#stability-levels-explained) and is disabled by default.
197+
### PausableComposition
198+
199+
> This feature is considered [Experimental](components-stability.md#stability-levels-explained).
194200
>
195201
{style="warning"}
196202

203+
**Default**: disabled
204+
205+
If enabled, changes the code generation of composable functions to allow pausing when part of a pausable composition.
206+
This lets Compose runtime suspend composition at skipping points,
207+
splitting long-running compositions across multiple frames.
208+
209+
Lazy lists and other performance intensive components use pausable composition to prefetch content
210+
that might cause visual jank when executed in a blocking manner.
211+
212+
> The feature flag affects behavior only with a version of Compose runtime that supports pausable composition,
213+
> starting with `androidx.compose.runtime` 1.8.0-alpha02.
214+
> Older versions ignore the feature flag.
215+
>
216+
{style="note"}
217+
197218
### StrongSkipping
198219

199220
**Default**: enabled

docs/topics/control-flow.md

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,82 @@ fun Request.getBody() =
260260

261261
The scope of a variable introduced as the subject is restricted to the body of the `when` expression or statement.
262262

263+
### Guard conditions in when expressions
264+
265+
> Guard conditions are an [experimental feature](components-stability.md#stability-levels-explained) that may be changed at any time.
266+
> We would appreciate your feedback in [YouTrack](https://youtrack.jetbrains.com/issue/KT-71140/Guard-conditions-in-when-expressions-feedback).
267+
>
268+
{style="warning"}
269+
270+
Guard conditions allow you to include
271+
more than one condition to the branches of a `when` expression, making complex control flow more explicit and concise.
272+
You can use guard conditions in `when` expressions or statements with a subject.
273+
274+
To include a guard condition in a branch, place it after the primary condition, separated by `if`:
275+
276+
```kotlin
277+
sealed interface Animal {
278+
data class Cat(val mouseHunter: Boolean) : Animal
279+
data class Dog(val breed: String) : Animal
280+
}
281+
282+
fun feedAnimal(animal: Animal) {
283+
when (animal) {
284+
// Branch with only primary condition. Calls `feedDog()` when `Animal` is `Dog`
285+
is Animal.Dog -> feedDog()
286+
// Branch with both primary and guard conditions. Calls `feedCat()` when `Animal` is `Cat` and is not `mouseHunter`
287+
is Animal.Cat if !animal.mouseHunter -> feedCat()
288+
// Prints "Unknown animal" if none of the above conditions match
289+
else -> println("Unknown animal")
290+
}
291+
}
292+
```
293+
294+
In a single `when` expression, you can combine branches with and without guard conditions.
295+
The code in a branch with a guard condition runs only if both the primary condition and the guard condition evaluate to true.
296+
If the primary condition does not match, the guard condition is not evaluated.
297+
298+
If you use guard conditions in `when` statements without an `else` branch, and none of the conditions matches, none of the branches is executed.
299+
300+
Otherwise, if you use guard conditions in `when` expressions without an `else` branch, the compiler requires you to declare all the possible cases to avoid runtime errors.
301+
302+
Additionally, guard conditions support `else if`:
303+
304+
```kotlin
305+
when (animal) {
306+
// Checks if `animal` is `Dog`
307+
is Animal.Dog -> feedDog()
308+
// Guard condition that checks if `animal` is `Cat` and not `mouseHunter`
309+
is Animal.Cat if !animal.mouseHunter -> feedCat()
310+
// Calls giveLettuce() if none of the above conditions match and animal.eatsPlants is true
311+
else if animal.eatsPlants -> giveLettuce()
312+
// Prints "Unknown animal" if none of the above conditions match
313+
else -> println("Unknown animal")
314+
}
315+
```
316+
317+
Combine multiple guard conditions within a single branch using the boolean operators `&&` (AND) or `||` (OR).
318+
Use parentheses around the boolean expressions to [avoid confusion](coding-conventions.md#guard-conditions-in-when-expression):
319+
320+
```kotlin
321+
when (animal) {
322+
is Animal.Cat if (!animal.mouseHunter && animal.hungry) -> feedCat()
323+
}
324+
```
325+
326+
You can use guard conditions in any `when` expression or statement with a subject, except the case when you have multiple conditions separated by a comma.
327+
For example, `0, 1 -> print("x == 0 or x == 1")`.
328+
329+
> To enable guard conditions in CLI, run the following command:
330+
>
331+
> `kotlinc -Xwhen-guards main.kt`
332+
>
333+
> To enable guard conditions in Gradle, add the following line to the `build.gradle.kts` file:
334+
>
335+
> `kotlin.compilerOptions.freeCompilerArgs.add("-Xwhen-guards")t`
336+
>
337+
{style="note"}
338+
263339
## For loops
264340

265341
The `for` loop iterates through anything that provides an iterator. This is equivalent to the `foreach` loop in languages like C#.

docs/topics/eap.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,9 @@ check [our instructions on how to configure your build to support this version](
4040

4141
## Build details
4242

43-
<!-- _No preview versions are currently available._ -->
43+
_No preview versions are currently available._
4444

45+
<!--
4546
<table>
4647
<tr>
4748
<th>Build info</th>
@@ -58,3 +59,4 @@ check [our instructions on how to configure your build to support this version](
5859
</td>
5960
</tr>
6061
</table>
62+
-->

docs/topics/faq.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ When targeting native, Kotlin will produce platform-specific code (via LLVM).
102102

103103
Kotlin lets you choose the version of JVM for execution. By default, the Kotlin/JVM compiler produces Java 8 compatible bytecode.
104104
If you want to make use of optimizations available in newer versions of Java, you can explicitly specify the target Java
105-
version from 9 to 21. Note that in this case the resulting bytecode might not run on lower versions.
105+
version from 9 to 23. Note that in this case the resulting bytecode might not run on lower versions.
106106
Starting with [Kotlin 1.5](whatsnew15.md#new-default-jvm-target-1-8), the compiler does not support producing bytecode compatible with Java versions below 8.
107107

108108
### Is Kotlin hard?

docs/topics/gradle/gradle-compilation-and-caches.md

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,21 @@ On this page, you can learn about the following topics:
1313

1414
## Incremental compilation
1515

16-
The Kotlin Gradle plugin supports incremental compilation. Incremental compilation tracks changes to files in the classpath
17-
between builds so that only the files affected by these changes are compiled. Incremental compilation works with [Gradle's
18-
build cache](#gradle-build-cache-support) and supports [compilation avoidance](https://docs.gradle.org/current/userguide/java_plugin.html#sec:java_compile_avoidance).
19-
20-
Incremental compilation is supported for Kotlin/JVM and Kotlin/JS projects, and is enabled by default.
16+
The Kotlin Gradle plugin supports incremental compilation, which is enabled by default for Kotlin/JVM and Kotlin/JS projects.
17+
Incremental compilation tracks changes to files in the classpath between builds so that only the files affected
18+
by these changes are compiled.
19+
This approach works with [Gradle's build cache](#gradle-build-cache-support) and supports [compilation avoidance](https://docs.gradle.org/current/userguide/java_plugin.html#sec:java_compile_avoidance).
20+
21+
For Kotlin/JVM, incremental compilation relies on classpath snapshots,
22+
which capture the API structure of modules to determine when recompilation is necessary.
23+
To optimize the overall pipeline, the Kotlin compiler uses two types of classpath snapshots:
24+
25+
* **Fine-grained snapshots:** include detailed information about class members, such as properties or functions.
26+
When member-level changes are detected, the Kotlin compiler recompiles only the classes that depend on the modified members.
27+
To maintain performance, the Kotlin Gradle plugin creates coarse-grained snapshots for `.jar` files in the Gradle cache.
28+
* **Coarse-grained snapshots:** only contain the class [ABI](https://en.wikipedia.org/wiki/Application_binary_interface) hash.
29+
When a part of ABI changes, the Kotlin compiler recompiles all classes that depend on the changed class.
30+
This is useful for classes that change infrequently, such as external libraries.
2131

2232
> Kotlin/JS projects use a different incremental compilation approach based on history files.
2333
>
@@ -31,14 +41,14 @@ There are several ways to disable incremental compilation:
3141

3242
The parameter should be added to each subsequent build.
3343

34-
Note: Any build with incremental compilation disabled invalidates incremental caches. The first build is never incremental.
44+
When you disable incremental compilation, incremental caches become invalid after the build. The first build is never incremental.
3545

3646
> Sometimes problems with incremental compilation become visible several rounds after the failure occurs. Use [build reports](#build-reports)
3747
> to track the history of changes and compilations. This can help you to provide reproducible bug reports.
3848
>
3949
{style="tip"}
4050

41-
If you'd like to learn more about how our current incremental compilation approach works and compares to the previous one,
51+
To learn more about how our current incremental compilation approach works and compares to the previous one,
4252
see our [blog post](https://blog.jetbrains.com/kotlin/2022/07/a-new-approach-to-incremental-compilation-in-kotlin/).
4353

4454
### Precise backup of compilation tasks' outputs
@@ -307,7 +317,7 @@ From Kotlin 2.0.0, the K2 compiler is used by default.
307317

308318
To use the previous compiler from Kotlin 2.0.0 onwards, either:
309319

310-
* In your `build.gradle.kts` file, [set your language version](gradle-compiler-options.md#example-of-setting-a-languageversion) to `1.9`.
320+
* In your `build.gradle.kts` file, [set your language version](gradle-compiler-options.md#example-of-setting-languageversion) to `1.9`.
311321

312322
OR
313323
* Use the following compiler option: `-language-version 1.9`.

0 commit comments

Comments
 (0)