Files
saselang-bible/examples/014-generics-examples.md
2026-09-12 18:12:13 +02:00

439 lines
5.1 KiB
Markdown

# Exemples / DO-DON'T — Chapitre 14 — Generics
> Document compagnon. Les exemples illustrent les règles du chapitre ; la formulation normative reste dans le chapitre lui-même.
## Exemples extraits du chapitre
### Exemple 1
```text
Array<T>
Result<T,E>
OpAdd<Lhs,Rhs,Out>
```
### Exemple 2
```text
class Box<T> {
...
}
struct Pair<A, B> {
...
}
interface Comparable<T> {
...
}
func identity<T>(T value) -> T {
return value;
}
```
### Exemple 3
```text
Box<int32>
Pair<String, uint64>
Result<Data, ParseError>
```
### Exemple 4
```text
Box
Result
```
### Exemple 5
```text
Box<int32> box = Box<int32>(42);
```
### Exemple 6
```text
func first<T>(T left, T right) -> T;
int32 a = ...;
int32 b = ...;
int32 c = first(a, b);
```
### Exemple 7
```text
func create<T>() -> T;
String value = create(); // ERROR
```
### Exemple 8
```text
String value = create<String>();
```
### Exemple 9
```text
func same<T>(T a, T b) -> bool;
int16 a = ...;
int32 b = ...;
same(a, b); // ERROR
same<int32>(a::toInt32(), b); // OK
```
### Exemple 10
```text
where T extends SomeClass
where T implements SomeInterface
```
### Exemple 11
```text
func process<T>(T value) -> Void
where T extends Entity
where T implements Serializable
where T implements Comparable<T>
{
...
}
```
### Exemple 12
```text
func compare<A, B>(A left, B right) -> Ordering
where A implements Comparable<B>
{
...
}
```
### Exemple 13
```text
where T implements Comparable<T>
```
### Exemple 14
```text
where T is numeric
where T is primitive
where T is struct
where T is class
```
### Exemple 15
```text
where T implements Numeric
```
### Exemple 16
```text
interface B extends A {
}
```
### Exemple 17
```text
where T implements B
where T implements A
```
### Exemple 18
```text
open class Base<T>
where T implements Serializable
{
}
class Child<T> extends Base<T>
where T implements Serializable
{
}
```
### Exemple 19
```text
class Box<T> implements Iterable<T> {
...
}
class StringList implements Iterable<String> {
...
}
class StringContainer extends Container<String> {
...
}
```
### Exemple 20
```text
class PairBox<T> implements Container<Pair<T, T>> {
...
}
```
### Exemple 21
```text
Consumer<String>
Consumer<Array<uint8>>
```
### Exemple 22
```text
même nombre de paramètres génériques
même nature type/const de chaque paramètre
mêmes relations de contraintes
même contrat callable après substitution
```
### Exemple 23
```text
Dog extends Animal
```
### Exemple 24
```text
Container<Dog> -> Container<Animal>
```
### Exemple 25
```text
in T
out T
+T
-T
wildcards Java
```
### Exemple 26
```text
monomorphisation
code partagé avec metadata
stratégie hybride
```
### Exemple 27
```text
struct StaticArray<T, const uint64 N> {
...
}
struct Matrix<T, const uint64 Rows, const uint64 Columns> {
...
}
```
### Exemple 28
```text
bool
char
uint8
uint16
uint32
uint64
uint128
uint256
enum simple sans payload
```
### Exemple 29
```text
int8..int256
float16..float128
String
class
struct arbitraire
tuple
enum avec payload
```
### Exemple 30
```text
littéraux
const nommées déjà résolues
paramètres const generic
parenthèses
opérateurs primitifs purs autorisés sur ces valeurs
```
### Exemple 31
```text
StaticArray<uint8, 32>
StaticArray<uint8, PAGE_SIZE>
StaticArray<uint8, PAGE_SIZE * 2>
StaticArray<uint8, 1u64 << 12>
```
### Exemple 32
```text
StaticArray<uint8, computeSize()> // ERROR
StaticArray<uint8, value::toUint64()> // ERROR
```
### Exemple 33
```text
fonction
méthode
constructeur
allocation
I/O
état runtime
exception
appel d'intrinsic exposé comme callable
```
### Exemple 34
```text
const uint64 SIZE = ...;
StaticArray<uint8, SIZE>
```
### Exemple 35
```text
StaticArray<T, 2 + 2>
StaticArray<T, 4>
```
### Exemple 36
```text
func length<T, const uint64 N>(StaticArray<T, N> array) -> uint64
```
### Exemple 37
```text
func strange<const uint64 N>(StaticArray<int32, N * 2> value)
```
### Exemple 38
```text
where N > 0
where N <= 1024
```
### Exemple 39
```text
class Node<T> {
Nullable<Node<T>> next;
}
```
### Exemple 40
```text
class BufferNode<T, const uint64 N> {
Nullable<BufferNode<T, N>> next;
}
```
### Exemple 41
```text
class Node<T> {
Nullable<Node<Array<T>>> next; // ERROR
}
```
### Exemple 42
```text
class BufferNode<T, const uint64 N> {
Nullable<BufferNode<T, N + 1>> next; // ERROR
}
```
### Exemple 43
```text
class A<T> {
Nullable<B<T>> b;
}
class B<T> {
Nullable<A<T>> a;
}
```
### Exemple 44
```text
class A<T> {
Nullable<B<Array<T>>> b;
}
class B<T> {
Nullable<A<T>> a;
}
```
### Exemple 45
```text
func recurse<T>(T value) -> Void {
recurse<T>(value); // OK
}
```
### Exemple 46
```text
func recurse<T>(T value) -> Void {
recurse<Array<T>>(...); // ERROR
}
```
### Exemple 47
```text
<T...>
<const uint64... N>
generic parameter packs
wildcards
specialization
variance déclarative
contraintes négatives
```
## DO / DON'T / WHY / compiler error / edge cases
À consolider progressivement avant la baseline publique V3 et à transformer, lorsque pertinent, en tests de conformité de la toolchain.