Skip to content

Commit 5fc90ff

Browse files
committed
update
1 parent 5afd2cf commit 5fc90ff

File tree

8 files changed

+149
-109
lines changed

8 files changed

+149
-109
lines changed

docs/src/0.language/README.md

Lines changed: 69 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,40 @@ fun main() {
455455
```
456456
:::
457457

458+
### Error Handling with Result<T>
459+
460+
Managing errors in Kotlin can be done using the `Result<T>` class, which allows you to handle success and failure cases without throwing exceptions.
461+
462+
::: kotlin-playground Result<T>
463+
464+
@file main.kt
465+
466+
```kotlin
467+
fun divide(a: Int, b: Int): Result<Double> {
468+
return if (b == 0) {
469+
Result.failure(Exception("Division par zéro"))
470+
} else {
471+
Result.success(a.toDouble() / b)
472+
}
473+
}
474+
475+
fun main() {
476+
val result = divide(10, 2)
477+
478+
result.fold(
479+
onSuccess = { value -> println("✅ Résultat: $value") },
480+
onFailure = { error -> println("❌ Erreur: ${error.message}") }
481+
)
482+
483+
// Avec erreur
484+
divide(10, 0).fold(
485+
onSuccess = { value -> println("✅ Résultat: $value") },
486+
onFailure = { error -> println("❌ Erreur: ${error.message}") }
487+
)
488+
}
489+
```
490+
:::
491+
458492
---
459493

460494
## Lesson 2: Functions
@@ -1457,7 +1491,7 @@ fun main() {
14571491

14581492
**Kotlin coroutines are a powerful tool for managing background tasks, making asynchronous programming easier and more efficient**. This comprehensive guide covers all aspects of concurrent programming in Kotlin.
14591493

1460-
### 4.1. Introduction to Coroutines
1494+
### Introduction to Coroutines
14611495

14621496
#### What are Coroutines?
14631497

@@ -1471,9 +1505,9 @@ fun main() {
14711505
- **Built-in cancellation support**: **Cancellation is propagated automatically through the running coroutine hierarchy**
14721506
- **Fewer memory leaks**: **Use structured concurrency to run operations within a scope**
14731507

1474-
### 4.2. Coroutine Builders
1508+
### Coroutine Builders
14751509

1476-
#### 4.2.1 Launch - Fire and Forget
1510+
#### Launch - Fire and Forget
14771511

14781512
::: kotlin-playground Launch Example
14791513

@@ -1498,7 +1532,7 @@ fun main() = runBlocking {
14981532
- Used for tasks that don't return a result
14991533
- Fire-and-forget operations
15001534

1501-
#### 4.2.2 Async - Result-Oriented
1535+
#### Async - Result-Oriented
15021536

15031537
**Launches a coroutine that returns a result asynchronously. Returns a `Deferred<T>`, which is like a Future in Java** :
15041538

@@ -1524,7 +1558,7 @@ fun main() = runBlocking {
15241558
```
15251559
:::
15261560

1527-
#### 4.2.3 RunBlocking - Bridging Blocking and Non-blocking
1561+
#### RunBlocking - Bridging Blocking and Non-blocking
15281562

15291563
::: kotlin-playground RunBlocking Example
15301564

@@ -1547,11 +1581,11 @@ fun main() = runBlocking {
15471581
```
15481582
:::
15491583

1550-
### 4.3. Dispatchers - Thread Management
1584+
### Dispatchers - Thread Management
15511585

15521586
**Dispatchers control where and how your coroutines run, like assigning them to specific threads or pools** .
15531587

1554-
#### 4.3.1 Types of Dispatchers
1588+
#### Types of Dispatchers
15551589

15561590
::: kotlin-playground Dispatchers Example
15571591

@@ -1583,7 +1617,7 @@ fun main() = runBlocking {
15831617
```
15841618
:::
15851619

1586-
#### 4.3.2 WithContext - Switching Context
1620+
#### WithContext - Switching Context
15871621

15881622
**withContext() calls the given code with the specified coroutine context, is suspended until it completes, and returns the result** :
15891623

@@ -1621,11 +1655,11 @@ fun main() = runBlocking {
16211655
```
16221656
:::
16231657

1624-
### 4.4. Structured Concurrency
1658+
### Structured Concurrency
16251659

16261660
**The CoroutineScope is the foundation of structured concurrency. It serves as a container for coroutines, defining the scope within which coroutines are launched** .
16271661

1628-
#### 4.4.1 CoroutineScope
1662+
#### CoroutineScope
16291663
::: kotlin-playground CoroutineScope Example
16301664

16311665
@file main.kt
@@ -1653,7 +1687,7 @@ fun main() = runBlocking {
16531687
```
16541688
:::
16551689

1656-
#### 4.4.2 Structured Hierarchy
1690+
#### Structured Hierarchy
16571691

16581692
**With structured concurrency, you can specify the major context elements (like dispatcher) once, when creating the top-level coroutine. All the nested coroutines then inherit the context and modify it only if needed** :
16591693

@@ -1694,7 +1728,7 @@ fun main() = runBlocking {
16941728
```
16951729
:::
16961730

1697-
#### 4.4.3 Supervision
1731+
#### Supervision
16981732

16991733
::: kotlin-playground Supervision Example
17001734

@@ -1731,11 +1765,11 @@ fun main() = runBlocking {
17311765
```
17321766
:::
17331767

1734-
### 4.5. Channels - Communication Between Coroutines
1768+
### Channels - Communication Between Coroutines
17351769

17361770
**Channels provide a way to share information between different coroutines** .
17371771

1738-
#### 4.5.1 Basic Channel Usage
1772+
#### Basic Channel Usage
17391773

17401774
::: kotlin-playground Basic Channel Example
17411775

@@ -1768,7 +1802,7 @@ fun main() = runBlocking {
17681802
:::
17691803

17701804

1771-
#### 4.5.2 Channel Types
1805+
#### Channel Types
17721806

17731807
**By default, a "Rendezvous" channel is created** :
17741808

@@ -1815,7 +1849,7 @@ fun main() {
18151849
}
18161850
```
18171851
:::
1818-
#### 4.5.3 Producer Pattern
1852+
#### Producer Pattern
18191853

18201854
::: kotlin-playground Producer Pattern Example
18211855

@@ -1848,11 +1882,11 @@ fun main() {
18481882
```
18491883
:::
18501884

1851-
### 4.6. Flow - Reactive Streams
1885+
### Flow - Reactive Streams
18521886

18531887
**Think of Flows as streams of data flowing through your server-side code, like the continuous stream of orders coming from the tables** .
18541888

1855-
#### 4.6.1 Basic Flow
1889+
#### Basic Flow
18561890

18571891
::: kotlin-playground Basic Flow Example
18581892

@@ -1877,7 +1911,7 @@ fun main() = runBlocking {
18771911
```
18781912
:::
18791913

1880-
#### 4.6.2 Flow Operators
1914+
#### Flow Operators
18811915

18821916
::: kotlin-playground Flow Operators Example
18831917

@@ -1896,7 +1930,7 @@ fun main() = runBlocking {
18961930
```
18971931
:::
18981932

1899-
#### 4.6.3 Cold vs Hot Flows
1933+
#### Cold vs Hot Flows
19001934

19011935
::: kotlin-playground Cold vs Hot Flows Example
19021936

@@ -1945,11 +1979,11 @@ fun main() {
19451979
:::
19461980

19471981

1948-
### 4.7. Exception Handling
1982+
### Exception Handling
19491983

19501984
**Structured concurrency enhances error handling by propagating exceptions up to the nearest exception handler in the coroutine hierarchy** .
19511985

1952-
#### 4.7.1 Try-Catch in Coroutines
1986+
#### Try-Catch in Coroutines
19531987

19541988
::: kotlin-playground Try-Catch Example
19551989

@@ -1976,7 +2010,7 @@ fun main() = runBlocking {
19762010
```
19772011
:::
19782012

1979-
#### 4.7.2 CoroutineExceptionHandler
2013+
#### CoroutineExceptionHandler
19802014

19812015
::: kotlin-playground Exception Handler Example
19822016

@@ -2001,9 +2035,9 @@ fun main() = runBlocking {
20012035
```
20022036
:::
20032037

2004-
### 4.8. Cancellation and Timeout
2038+
### Cancellation and Timeout
20052039

2006-
#### 4.8.1 Cooperative Cancellation
2040+
#### Cooperative Cancellation
20072041

20082042
::: kotlin-playground Cancellation Example
20092043

@@ -2034,7 +2068,7 @@ fun main() = runBlocking {
20342068
```
20352069
:::
20362070

2037-
#### 4.8.2 Timeout
2071+
#### Timeout
20382072

20392073
::: kotlin-playground Timeout Example
20402074

@@ -2058,11 +2092,11 @@ fun main() = runBlocking {
20582092
```
20592093
:::
20602094

2061-
### 4.9. Thread Safety and Synchronization
2095+
### Thread Safety and Synchronization
20622096

20632097
**One approach to addressing shared mutable state is by using thread-safe data structures provided by the Kotlin standard library, such as Atomic types** .
20642098

2065-
#### 4.9.1 Atomic Operations
2099+
#### Atomic Operations
20662100

20672101
::: kotlin-playground Atomic Operations Example
20682102

@@ -2089,7 +2123,7 @@ fun main() = runBlocking {
20892123
```
20902124
:::
20912125

2092-
#### 4.9.2 Mutex
2126+
#### Mutex
20932127

20942128
::: kotlin-playground Mutex Example
20952129

@@ -2125,7 +2159,7 @@ fun main() = runBlocking {
21252159

21262160
:::
21272161

2128-
### 5.1 Explicit Backing Fields ([Issue 278](https://github.com/Kotlin/KEEP/issues/278))
2162+
### Explicit Backing Fields ([Issue 278](https://github.com/Kotlin/KEEP/issues/278))
21292163

21302164

21312165

@@ -2154,7 +2188,7 @@ fun main() {
21542188
}
21552189
```
21562190

2157-
### 5.2 Collection Literals ([KT-43871](https://youtrack.jetbrains.com/issue/KT-43871))
2191+
### Collection Literals ([KT-43871](https://youtrack.jetbrains.com/issue/KT-43871))
21582192

21592193
```kotlin
21602194
// Proposed collection literals syntax (not yet implemented)
@@ -2185,7 +2219,7 @@ fun main() {
21852219
}
21862220
```
21872221

2188-
### 5.3 Name based destructuring ([Kotlin 2.4](https://kotlinlang.org/docs/destructuring-declarations.html))
2222+
### Name based destructuring ([Kotlin 2.4](https://kotlinlang.org/docs/destructuring-declarations.html))
21892223

21902224

21912225
```kotlin
@@ -2209,7 +2243,7 @@ fun main() {
22092243
}
22102244
```
22112245

2212-
### 5.4 Rich Errors ([KT-68296](https://youtrack.jetbrains.com/issue/KT-68296))
2246+
### Rich Errors ([KT-68296](https://youtrack.jetbrains.com/issue/KT-68296))
22132247

22142248
```kotlin
22152249
// Rich errors with union types (proposed syntax)
@@ -2249,7 +2283,7 @@ fun main() {
22492283
}
22502284
```
22512285

2252-
### 5.5 Must return values
2286+
### Must return values
22532287

22542288
```kotlin
22552289
// Must-use return values (proposed syntax)
@@ -2288,7 +2322,7 @@ fun main() {
22882322
```
22892323
:::
22902324

2291-
### 5.6 - Additional Kotlin Language Features
2325+
### Additional Kotlin Language Features
22922326

22932327
| Feature | Description | Status | Example Use Case |
22942328
|---------|-------------|--------|------------------|
@@ -2304,7 +2338,5 @@ fun main() {
23042338
|----------|---------|
23052339
| [github.com/Kotlin/KEEP](https://github.com/Kotlin/KEEP) | Official Kotlin Enhancement Proposals - track feature development and proposals |
23062340
| [x.com/kotlin](https://x.com/kotlin) | Official Kotlin Twitter/X account - announcements, updates, and community news |
2307-
23082341
---
2309-
23102342
:::

0 commit comments

Comments
 (0)