-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
58 changed files
with
1,070 additions
and
1,062 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
<?php | ||
|
||
/** | ||
* @param any $expr | ||
*/ | ||
function expr_type($expr, string $string) { | ||
} | ||
|
||
/** | ||
* @kphp-generic T | ||
* @param T $arg | ||
* @return T | ||
*/ | ||
function nullable_of($arg) { | ||
if (0) { | ||
return null; | ||
} | ||
return $arg; | ||
} | ||
|
||
/** | ||
* @param mixed $args | ||
*/ | ||
function tuple(...$args) { | ||
return ${"args"}; | ||
} | ||
/** | ||
* @param mixed $args | ||
*/ | ||
function shape($args) { | ||
return ${"args"}; | ||
} | ||
|
||
/** | ||
* @kphp-generic T | ||
* @param T ...$els | ||
* @return SimpleVector<T> | ||
*/ | ||
function listOf(...$els) { | ||
return new SimpleVector(...$els); | ||
} | ||
|
||
/** | ||
* @kphp-generic T | ||
* @param T $arg | ||
* @return T | ||
*/ | ||
function mirror($arg) { | ||
return $arg; | ||
} | ||
|
||
/** | ||
* @kphp-generic T1, T2 | ||
* @param T1 $a1 | ||
* @param T2 $a2 | ||
* @return T1|T2 | ||
*/ | ||
function combine($a1, $a2) { | ||
if (0) { | ||
return $a2; | ||
} | ||
return $a1; | ||
} | ||
|
||
/** | ||
* @kphp-generic T1, T2 | ||
* @param T1[] $array | ||
* @param class-string<T2> $class | ||
* @return T2[] | ||
*/ | ||
function filter_is_instance($array, $class) { | ||
return array_filter($array, fn($el) => is_a($el, $class));; | ||
} | ||
|
||
/** | ||
* @kphp-generic T | ||
* @param callable(T,T): bool $gt | ||
* @param T ...$arr | ||
* @return T | ||
*/ | ||
function max_by($gt, ...$arr) { | ||
$max = array_first_value($arr); | ||
for ($i = 1; $i < count($arr); ++$i) { | ||
if ($gt($arr[$i], $max)) { | ||
$max = $arr[$i]; | ||
} | ||
} | ||
return $max; | ||
} | ||
|
||
/** | ||
* @kphp-generic T, DstClass | ||
* @param T $obj | ||
* @param class-string<DstClass> $to_classname | ||
* @return DstClass | ||
*/ | ||
function instance_cast($obj, $to_classname) { | ||
return $obj; | ||
} | ||
|
||
/** | ||
* @kphp-generic TElem, ToName | ||
* @param TElem[] $arr | ||
* @param class-string<ToName> $to | ||
* @return ToName[] | ||
*/ | ||
function array_cast($arr, $to) { | ||
$out = []; | ||
foreach ($arr as $k => $v) { | ||
$out[$k] = instance_cast($v, $to); | ||
} | ||
return $out; | ||
} | ||
|
||
/** | ||
* @kphp-generic T | ||
* @param T[] $arr | ||
* @return T | ||
*/ | ||
function array_first_value(array $arr) { | ||
return $arr[0]; | ||
} |
19 changes: 19 additions & 0 deletions
19
src/test/fixtures/generics/Containers/MutableVectorList.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
/** | ||
* Mutable generic collection. | ||
* | ||
* @kphp-generic T | ||
*/ | ||
class MutableList extends VectorList { | ||
/** | ||
* @param T $data | ||
*/ | ||
public function add($data): void { | ||
$this->data[] = $data; | ||
} | ||
|
||
public function remove(int $index): void { | ||
unset($this->data[$index]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
/** | ||
* @kphp-generic T1, T2 | ||
*/ | ||
class Pair { | ||
/** @var T1 */ | ||
private $first; | ||
|
||
/** @var T2 */ | ||
private $second; | ||
|
||
/** | ||
* @param T1 $first | ||
* @param T2 $second | ||
*/ | ||
public function __construct($first, $second) { | ||
$this->first = $first; | ||
$this->second = $second; | ||
} | ||
|
||
/** | ||
* @return T1 | ||
*/ | ||
public function first() { | ||
return $this->first; | ||
} | ||
|
||
/** | ||
* @return T2 | ||
*/ | ||
public function second() { | ||
return $this->second; | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
src/test/fixtures/generics/Containers/SerializableKeyMap.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
/** | ||
* @kphp-generic TKey: Serializable, TValue | ||
*/ | ||
class SerializableKeyMap { | ||
/** @var TValue[] */ | ||
private $data = []; | ||
|
||
/** | ||
* @param Pair<TKey, TValue> ...$els | ||
*/ | ||
public function __construct(...$els) { | ||
foreach ($els as $keyValue) { | ||
$key = $keyValue->first(); | ||
$this->data[$key->serialize()] = $keyValue->second(); | ||
} | ||
} | ||
|
||
/** | ||
* @param TKey $key | ||
* @return TValue | ||
*/ | ||
public function get($key) { | ||
return $this->data[$key->serialize()]; | ||
} | ||
|
||
/** | ||
* @param TKey $key | ||
* @param TValue $value | ||
*/ | ||
public function set($key, $value): void { | ||
$this->data[$key->serialize()] = $key; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
/** | ||
* @kphp-generic TKey, TValue | ||
*/ | ||
class SimpleMap { | ||
/** @var TValue[] */ | ||
private $data = []; | ||
|
||
/** | ||
* @param Pair<TKey, TValue> ...$els | ||
*/ | ||
public function __construct(...$els) { | ||
foreach ($els as $keyValue) { | ||
$key = $keyValue->first(); | ||
$this->data[$key->serialize()] = $keyValue->second(); | ||
} | ||
} | ||
|
||
/** | ||
* @param TKey $key | ||
* @return TValue | ||
*/ | ||
public function get($key) { | ||
return $this->data[$key->serialize()]; | ||
} | ||
|
||
/** | ||
* @param TKey $key | ||
* @param TValue $value | ||
*/ | ||
public function set($key, $value): void { | ||
$this->data[$key->serialize()] = $key; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
/** | ||
* @kphp-generic T | ||
*/ | ||
class SimpleVector { | ||
/** @var T[] */ | ||
private $data = []; | ||
|
||
/** | ||
* @param T ...$els | ||
*/ | ||
public function __construct(...$els) { | ||
$this->data = $els; | ||
} | ||
|
||
/** | ||
* @return T | ||
*/ | ||
public function get(int $index) { | ||
return $this->data[$index]; | ||
} | ||
|
||
/** | ||
* @param T $data | ||
*/ | ||
public function add($data): void { | ||
$this->data[] = $data; | ||
} | ||
|
||
/** | ||
* @return T[] | ||
*/ | ||
public function raw(): array { | ||
return $this->data; | ||
} | ||
} |
Oops, something went wrong.