# 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 Result OpAdd ``` ### Exemple 2 ```text class Box { ... } struct Pair { ... } interface Comparable { ... } func identity(T value) -> T { return value; } ``` ### Exemple 3 ```text Box Pair Result ``` ### Exemple 4 ```text Box Result ``` ### Exemple 5 ```text Box box = Box(42); ``` ### Exemple 6 ```text func first(T left, T right) -> T; int32 a = ...; int32 b = ...; int32 c = first(a, b); ``` ### Exemple 7 ```text func create() -> T; String value = create(); // ERROR ``` ### Exemple 8 ```text String value = create(); ``` ### Exemple 9 ```text func same(T a, T b) -> bool; int16 a = ...; int32 b = ...; same(a, b); // ERROR same(a::toInt32(), b); // OK ``` ### Exemple 10 ```text where T extends SomeClass where T implements SomeInterface ``` ### Exemple 11 ```text func process(T value) -> Void where T extends Entity where T implements Serializable where T implements Comparable { ... } ``` ### Exemple 12 ```text func compare(A left, B right) -> Ordering where A implements Comparable { ... } ``` ### Exemple 13 ```text where T implements Comparable ``` ### 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 where T implements Serializable { } class Child extends Base where T implements Serializable { } ``` ### Exemple 19 ```text class Box implements Iterable { ... } class StringList implements Iterable { ... } class StringContainer extends Container { ... } ``` ### Exemple 20 ```text class PairBox implements Container> { ... } ``` ### Exemple 21 ```text Consumer Consumer> ``` ### 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 -> Container ``` ### 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 { ... } struct Matrix { ... } ``` ### 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 StaticArray StaticArray StaticArray ``` ### Exemple 32 ```text StaticArray // ERROR StaticArray // 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 ``` ### Exemple 35 ```text StaticArray StaticArray ``` ### Exemple 36 ```text func length(StaticArray array) -> uint64 ``` ### Exemple 37 ```text func strange(StaticArray value) ``` ### Exemple 38 ```text where N > 0 where N <= 1024 ``` ### Exemple 39 ```text class Node { Nullable> next; } ``` ### Exemple 40 ```text class BufferNode { Nullable> next; } ``` ### Exemple 41 ```text class Node { Nullable>> next; // ERROR } ``` ### Exemple 42 ```text class BufferNode { Nullable> next; // ERROR } ``` ### Exemple 43 ```text class A { Nullable> b; } class B { Nullable> a; } ``` ### Exemple 44 ```text class A { Nullable>> b; } class B { Nullable> a; } ``` ### Exemple 45 ```text func recurse(T value) -> Void { recurse(value); // OK } ``` ### Exemple 46 ```text func recurse(T value) -> Void { recurse>(...); // ERROR } ``` ### Exemple 47 ```text 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.