288 lines
3.4 KiB
Markdown
288 lines
3.4 KiB
Markdown
# Exemples / DO-DON'T — Chapitre 19 — Contrôle de flux
|
|
|
|
> Document compagnon non normatif tant qu'une règle n'est pas explicitement référencée comme normative par le chapitre.
|
|
|
|
## Exemples actuellement présents dans le chapitre
|
|
|
|
### Exemple 1
|
|
|
|
```text
|
|
if (condition) {
|
|
action();
|
|
} elseif (otherCondition) {
|
|
otherAction();
|
|
} else {
|
|
fallback();
|
|
}
|
|
```
|
|
|
|
### Exemple 2
|
|
|
|
```text
|
|
int32 result = if (x > 10) {
|
|
emit 1;
|
|
} elseif (x > 5) {
|
|
emit 2;
|
|
} else {
|
|
emit 3;
|
|
};
|
|
```
|
|
|
|
### Exemple 3
|
|
|
|
```text
|
|
Option<uint32> result = if (condition) {
|
|
emit Option::Some(42);
|
|
} else {
|
|
emit Option::None;
|
|
};
|
|
```
|
|
|
|
### Exemple 4
|
|
|
|
```text
|
|
match (value) {
|
|
pattern1 => {
|
|
statements;
|
|
}
|
|
|
|
pattern2 => {
|
|
statements;
|
|
}
|
|
|
|
_ => {
|
|
statements;
|
|
}
|
|
}
|
|
```
|
|
|
|
### Exemple 5
|
|
|
|
```text
|
|
literal pattern
|
|
enum variant pattern
|
|
tuple pattern
|
|
struct pattern
|
|
typed binding pattern
|
|
wildcard _
|
|
alternative pattern avec |
|
|
nested pattern
|
|
Range pattern si et seulement si le Range est valide selon les règles de Range
|
|
```
|
|
|
|
### Exemple 6
|
|
|
|
```text
|
|
1 | 2 | 3 => {
|
|
...
|
|
}
|
|
```
|
|
|
|
### Exemple 7
|
|
|
|
```text
|
|
A(uint32 value) | B(uint32 value) => {
|
|
use(value);
|
|
}
|
|
```
|
|
|
|
### Exemple 8
|
|
|
|
```text
|
|
Color::Red => {
|
|
...
|
|
}
|
|
```
|
|
|
|
### Exemple 9
|
|
|
|
```text
|
|
Result::Ok(uint32 value) => {
|
|
use(value);
|
|
}
|
|
|
|
Result::Err(Error error) => {
|
|
handle(error);
|
|
}
|
|
```
|
|
|
|
### Exemple 10
|
|
|
|
```text
|
|
Result::Ok(_) => {
|
|
...
|
|
}
|
|
```
|
|
|
|
### Exemple 11
|
|
|
|
```text
|
|
(0, 0) => {
|
|
origin();
|
|
}
|
|
|
|
(int32 x, 0) => {
|
|
horizontal(x);
|
|
}
|
|
|
|
(int32 x, int32 y) => {
|
|
other(x, y);
|
|
}
|
|
```
|
|
|
|
### Exemple 12
|
|
|
|
```text
|
|
Point {
|
|
x: 0,
|
|
y: int32 vertical
|
|
} => {
|
|
use(vertical);
|
|
}
|
|
```
|
|
|
|
### Exemple 13
|
|
|
|
```text
|
|
Point {
|
|
x: 0,
|
|
y: _
|
|
}
|
|
```
|
|
|
|
### Exemple 14
|
|
|
|
```text
|
|
SomeEnum::PointValue(
|
|
Point {
|
|
x: 0,
|
|
y: int32 y
|
|
}
|
|
) => {
|
|
use(y);
|
|
}
|
|
```
|
|
|
|
### Exemple 15
|
|
|
|
```text
|
|
int32 result = match (value) {
|
|
0 => {
|
|
emit 10;
|
|
}
|
|
|
|
_ => {
|
|
emit 20;
|
|
}
|
|
};
|
|
```
|
|
|
|
### Exemple 16
|
|
|
|
```text
|
|
int32 result;
|
|
|
|
result = if (condition) {
|
|
emit 1;
|
|
} else {
|
|
emit 2;
|
|
};
|
|
```
|
|
|
|
### Exemple 17
|
|
|
|
```text
|
|
return V1 requis
|
|
break V1 requis
|
|
continue V1 requis
|
|
yield réservé pour generators/coroutines si non finalisé en V1
|
|
```
|
|
|
|
### Exemple 18
|
|
|
|
```text
|
|
while (condition) {
|
|
...
|
|
}
|
|
|
|
dowhile (condition) {
|
|
...
|
|
}
|
|
|
|
for (initialization; condition; update) {
|
|
...
|
|
}
|
|
|
|
foreach (Type item in iterable) {
|
|
...
|
|
}
|
|
```
|
|
|
|
### Exemple 19
|
|
|
|
```text
|
|
dowhile (condition) {
|
|
statements;
|
|
}
|
|
```
|
|
|
|
### Exemple 20
|
|
|
|
```text
|
|
for (;;) {
|
|
...
|
|
}
|
|
```
|
|
|
|
### Exemple 21
|
|
|
|
```text
|
|
while (true) {
|
|
...
|
|
}
|
|
```
|
|
|
|
### Exemple 22
|
|
|
|
```text
|
|
for (i = 0, j = 5; i < x || j < y; i += 1, j += 1) {
|
|
...
|
|
}
|
|
```
|
|
|
|
### Exemple 23
|
|
|
|
```text
|
|
foreach (Type item in iterable) {
|
|
...
|
|
}
|
|
```
|
|
|
|
### Exemple 24
|
|
|
|
```text
|
|
until (...) -> utiliser while (!...)
|
|
repeat ... until -> utiliser dowhile (!...)
|
|
unless (...) -> utiliser if (!...)
|
|
loop { ... } -> utiliser while (true) { ... }
|
|
```
|
|
|
|
### Exemple 25
|
|
|
|
```text
|
|
;
|
|
;;
|
|
foo();;
|
|
```
|
|
|
|
### Exemple 26
|
|
|
|
```text
|
|
foo(first, second, third); // valide
|
|
foo(first, second, third,); // invalide
|
|
```
|
|
|
|
## DO / DON'T / WHY / compiler error / edge cases
|
|
|
|
La couverture structurée de cette section sera enrichie au fur et à mesure de la fermeture des règles du chapitre. La migration `0.2.12` conserve volontairement les exemples historiques dans le chapitre afin de ne perdre aucun contexte normatif.
|