<?hh //strict
interface Comparer<T> {
public function __invoke(T $a, T $b): int;
}
class Box<T> {
public T $property;
public function __construct(T $property){
$this->property = $property;
}
}
class BoxComparer<T> implements Comparer<Box<T>> {
public function __invoke(Box<T> $a, Box<T> $b): int {
if ($a->property < $b->property) {
return -1;
} elseif ($b->property < $a->property) {
return 1;
} else {
return 0;
}
}
}
class Sorter<T> {
public function __invoke(Collection<T> $items, (function(T, T): int) $comparer): void {
echo __METHOD__, PHP_EOL;
}
}
$collection = Vector {new Box(1), new Box(2)};
$sorter = new Sorter();
$sorter($collection, new BoxComparer());
$sorter($collection, 1);
- Output for 5.4.0 - 5.4.32, 7.2.29 - 7.2.33, 7.3.16 - 7.3.31, 7.4.3 - 7.4.32, 8.0.0 - 8.0.12, 8.0.14 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
- <?hh //strict
interface Comparer<T> {
public function __invoke(T $a, T $b): int;
}
class Box<T> {
public T $property;
public function __construct(T $property){
$this->property = $property;
}
}
class BoxComparer<T> implements Comparer<Box<T>> {
public function __invoke(Box<T> $a, Box<T> $b): int {
if ($a->property < $b->property) {
return -1;
} elseif ($b->property < $a->property) {
return 1;
} else {
return 0;
}
}
}
class Sorter<T> {
public function __invoke(Collection<T> $items, (function(T, T): int) $comparer): void {
echo __METHOD__, PHP_EOL;
}
}
$collection = Vector {new Box(1), new Box(2)};
$sorter = new Sorter();
$sorter($collection, new BoxComparer());
$sorter($collection, 1);
- Output for 8.0.13
- Parse error: syntax error, unexpected token "interface" in /in/8sjT2 on line 3
Process exited with code 255. - Output for 5.4.34 - 5.4.35, 7.3.32 - 7.3.33, 7.4.33
- Parse error: syntax error, unexpected 'interface' (T_INTERFACE) in /in/8sjT2 on line 3
Process exited with code 255.
preferences:
175.9 ms | 402 KiB | 174 Q