5.1 KiB
5.1 KiB
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
Array<T>
Result<T,E>
OpAdd<Lhs,Rhs,Out>
Exemple 2
class Box<T> {
...
}
struct Pair<A, B> {
...
}
interface Comparable<T> {
...
}
func identity<T>(T value) -> T {
return value;
}
Exemple 3
Box<int32>
Pair<String, uint64>
Result<Data, ParseError>
Exemple 4
Box
Result
Exemple 5
Box<int32> box = Box<int32>(42);
Exemple 6
func first<T>(T left, T right) -> T;
int32 a = ...;
int32 b = ...;
int32 c = first(a, b);
Exemple 7
func create<T>() -> T;
String value = create(); // ERROR
Exemple 8
String value = create<String>();
Exemple 9
func same<T>(T a, T b) -> bool;
int16 a = ...;
int32 b = ...;
same(a, b); // ERROR
same<int32>(a::toInt32(), b); // OK
Exemple 10
where T extends SomeClass
where T implements SomeInterface
Exemple 11
func process<T>(T value) -> Void
where T extends Entity
where T implements Serializable
where T implements Comparable<T>
{
...
}
Exemple 12
func compare<A, B>(A left, B right) -> Ordering
where A implements Comparable<B>
{
...
}
Exemple 13
where T implements Comparable<T>
Exemple 14
where T is numeric
where T is primitive
where T is struct
where T is class
Exemple 15
where T implements Numeric
Exemple 16
interface B extends A {
}
Exemple 17
where T implements B
where T implements A
Exemple 18
open class Base<T>
where T implements Serializable
{
}
class Child<T> extends Base<T>
where T implements Serializable
{
}
Exemple 19
class Box<T> implements Iterable<T> {
...
}
class StringList implements Iterable<String> {
...
}
class StringContainer extends Container<String> {
...
}
Exemple 20
class PairBox<T> implements Container<Pair<T, T>> {
...
}
Exemple 21
Consumer<String>
Consumer<Array<uint8>>
Exemple 22
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
Dog extends Animal
Exemple 24
Container<Dog> -> Container<Animal>
Exemple 25
in T
out T
+T
-T
wildcards Java
Exemple 26
monomorphisation
code partagé avec metadata
stratégie hybride
Exemple 27
struct StaticArray<T, const uint64 N> {
...
}
struct Matrix<T, const uint64 Rows, const uint64 Columns> {
...
}
Exemple 28
bool
char
uint8
uint16
uint32
uint64
uint128
uint256
enum simple sans payload
Exemple 29
int8..int256
float16..float128
String
class
struct arbitraire
tuple
enum avec payload
Exemple 30
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
StaticArray<uint8, 32>
StaticArray<uint8, PAGE_SIZE>
StaticArray<uint8, PAGE_SIZE * 2>
StaticArray<uint8, 1u64 << 12>
Exemple 32
StaticArray<uint8, computeSize()> // ERROR
StaticArray<uint8, value::toUint64()> // ERROR
Exemple 33
fonction
méthode
constructeur
allocation
I/O
état runtime
exception
appel d'intrinsic exposé comme callable
Exemple 34
const uint64 SIZE = ...;
StaticArray<uint8, SIZE>
Exemple 35
StaticArray<T, 2 + 2>
StaticArray<T, 4>
Exemple 36
func length<T, const uint64 N>(StaticArray<T, N> array) -> uint64
Exemple 37
func strange<const uint64 N>(StaticArray<int32, N * 2> value)
Exemple 38
where N > 0
where N <= 1024
Exemple 39
class Node<T> {
Nullable<Node<T>> next;
}
Exemple 40
class BufferNode<T, const uint64 N> {
Nullable<BufferNode<T, N>> next;
}
Exemple 41
class Node<T> {
Nullable<Node<Array<T>>> next; // ERROR
}
Exemple 42
class BufferNode<T, const uint64 N> {
Nullable<BufferNode<T, N + 1>> next; // ERROR
}
Exemple 43
class A<T> {
Nullable<B<T>> b;
}
class B<T> {
Nullable<A<T>> a;
}
Exemple 44
class A<T> {
Nullable<B<Array<T>>> b;
}
class B<T> {
Nullable<A<T>> a;
}
Exemple 45
func recurse<T>(T value) -> Void {
recurse<T>(value); // OK
}
Exemple 46
func recurse<T>(T value) -> Void {
recurse<Array<T>>(...); // ERROR
}
Exemple 47
<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.