CLERMONT'ECH API HOUR #56
Les génériques permettent de rendre paramétrables les types des classes, interfaces et méthodes...
Écrire du code typé mais sans être spécifiquement lié à un type.
Ajouter une couche d'abstraction sur les types.
Pas de cast (ou documentation)
Optimise et simplifie (?)
Facilite la maintenance du code
Augmente la sécurité du code
Notation pas (toujours) simple
Performances au runtime (?)
Concept important
Souvent natif (Java, Go, Rust, ...)
Très attendu
class Collection
{
public function push(??? $item): void {}
public function pop(): ??? {}
}
class TypedCollection
{
private string $type;
public function push(??? $item): void {}
public function pop(): ??? {}
}
class FooCollection
{
public function push(Foo $item): void {}
public function pop(): Foo {}
}
function add(??? $a, ??? $b): ???
{
return $a + $b;
}
$result = add(1, 42); // is a int
$result = add(2.21, 3.14); // is a float
function transformToArray(??? $a, ??? $b): array
{
return [$a, $b];
}
$result = transformToArray(1, 42); // is a int[]
$result = transformToArray(2.21, 3.14); // is a float[]
function make(string $className): ???
{
return new $className();
}
$result = make(Foo::class); // is a Foo
$result = make(Exception::class); // is an Exception
func SumIntsOrFloats[K comparable, V int64 | float64](m map[K]V) V {
var s V
for _, v := range m {
s += v
}
return s
}
SumIntsOrFloats[string, int64](ints)
SumIntsOrFloats[string, float64](floats)
(parce que Nikita l'a dit)
// i can take every param and return what i want
function identity($value)
{
return $value;
}
$r1 = identity(2023);
$r2 = identity('Symfony Live Paris');
$r3 = identity(true);
$r4 = identity(['Symfony Live Paris']);
echo $r4[0];
Si on utilisait ça ?
function identity(mixed $value): mixed
{
return $value;
}
$r1 = identity(2023);
$r2 = identity('Symfony Live Paris');
$r3 = identity(true);
$r4 = identity(['Symfony Live Paris']);
echo $r4[0];
Les annotations !
la syntaxe sous forme de commentaire
coloration syntaxique quasi inexistante
validation du contenu difficile
...
/*
/**
* @template T
* @param T $value
* @return T
*/
function identity(mixed $value): mixed
{
return $value;
}
$r1 = identity(2023);
$r2 = identity('Symfony Live Paris');
$r3 = identity(true);
$r4 = identity(['Symfony Live Paris']);
echo $r4[0];
/**
* @template T
* @param T $value
* @return T
*/
function identity(mixed $value): mixed
{
return [$value];
}
$r1 = identity(2023);
$r2 = identity('Symfony Live Paris');
$r3 = identity(true);
$r4 = identity(['Symfony Live Paris']);
echo $r1[0]; // 2023
echo $r2[0]; // Symfony Live Paris
echo $r3[0]; // 1
echo $r4[0]; // Warning: Array to string conversion
/**
* @template T
*/
interface Collection
{
/**
* @param T $item
*/
public function push(mixed $item): void;
/**
* @return T
*/
public function pop(): mixed;
}
/**
* @template K
* @template V
*/
interface Collection
{
/**
* @param V $item
*/
public function push(mixed $item): void;
/**
* @return V
*/
public function pop(): mixed;
/**
* @param K $index
* @return V
*/
public function get(mixed $index): mixed;
}
/**
* @template T of \Exception
* @param T $value
* @return T
*/
function identity($value): mixed
{
return $value;
}
$r1 = identity(new LogicException());
assert($r1 instanceof LogicException);
/**
* @template T of \Exception
* @param T $value
* @return T
*/
function identity($value): mixed
{
return $value;
}
$r1 = identity('Hello Symfony Live Paris!');
assert(is_string($r1));
/**
* @template T of object
* @param class-string<T> $className
* @return T
*/
function make(string $className): object
{
return new $className;
}
$e = make(Exception::class);
echo $e->getMessage();
function make(string $className): object
{
return new $className;
}
$e = make(Exception::class);
echo $e->getMessage();
/**
* @template T
*/
interface Repository
{
/** @param T $item */
public function save($item): void;
/** @return T */
public function get($id): mixed;
}
/** @implements Repository<Bar> */
class BarRepository implements Repository
{
public function add($item): void {}
public function get($id): mixed
{
return $item;
}
}
/**
* @template T
*/
interface Repository
{
/** @param T $item */
public function save($item): void;
/** @return T */
public function get($id): mixed;
}
/** @implements Repository<Bar> */
class BarRepository implements Repository
{
public function add($item): void {}
public function get($id): mixed
{
return 'Hey';
}
}
/**
* @template T
*/
class Collection
{
/** T $item */
protected mixed $item;
/** @param T $item */
public function add($item): void {$this->item = $item;}
/** @return T */
public function get(): mixed { return $this->item;}
}
/** @extends Collection<Bar> */
class BarCollection extends Collection
{
public function add($item): void {$this->item = $item;}
public function get(): mixed
{
return new Bar();
}
}
/**
* @template T
*/
class Collection
{
/** T $item */
protected mixed $item;
/** @return T */
public function get(): mixed { return $this->item;}
}
/** @extends Collection<Bar> */
class BarCollection extends Collection
{
public function get(): mixed
{
return 'Heyyy';
}
}
Pas de surcoût au runtime
Couverture fonctionnelle
Source de vérité
PHP + annotations + attributs
Pas d'obligation
/**
* Return the given value, optionally passed through the given callback.
*
* @template TValue
* @template TReturn
*
* @param TValue $value
* @param (callable(TValue): (TReturn))|null $callback
* @return ($callback is null ? TValue : TReturn)
*/
function with($value, callable $callback = null)
{
return is_null($callback) ? $value : $callback($value);
}
Concept important (collection, ...)
Pas natif en PHP
Très bien supporté par des outils
https://slides.com/kpn13
@kpn13
https://blog.karimpinchon.com