-
Notifications
You must be signed in to change notification settings - Fork 7.9k
[PoC] Limited Abstract Generics #18260
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
e6fdff1
a192a3a
9304a82
066f04f
b7cf20b
0ec2ea6
e215b8b
e6a1a7e
d252759
1691402
139bbf6
2b9f0f5
b1a3546
363f817
2198aaf
be7efd5
87f9544
56f2f1c
2d83b54
51b689e
cad9308
5e076d0
beabf2b
36a4736
7fbdc0a
6cdc4f5
7b38d21
20e21ab
92a45c7
d1bae38
518d7ea
9d17cae
00372bd
02695e0
6fd8860
ad1ae05
fba8f76
106c3d2
439458c
229fcc9
891f24d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
--TEST-- | ||
Abstract generic types basic | ||
--FILE-- | ||
<?php | ||
|
||
interface I<T> { | ||
public function foo(T $param): T; | ||
} | ||
|
||
class CS implements I<string> { | ||
public function foo(string $param): string { | ||
return $param . '!'; | ||
} | ||
} | ||
|
||
class CI implements I<int> { | ||
public function foo(int $param): int { | ||
return $param + 42; | ||
} | ||
} | ||
|
||
$cs = new CS(); | ||
var_dump($cs->foo("Hello")); | ||
|
||
$ci = new CI(); | ||
var_dump($ci->foo(5)); | ||
|
||
?> | ||
--EXPECT-- | ||
string(6) "Hello!" | ||
int(47) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
--TEST-- | ||
Concrete example of using AT | ||
--CREDITS-- | ||
Levi Morrison | ||
--FILE-- | ||
<?php declare(strict_types=1); | ||
|
||
namespace Sequence; | ||
|
||
// No null. This is probably going to be painful, but let's try it. | ||
interface Sequence<Item : object|array|string|float|int|bool> | ||
{ | ||
function next(): ?Item; | ||
|
||
/** | ||
* @param callable(Item, Item): Item $f | ||
* @return ?Item | ||
*/ | ||
function reduce(callable $f): ?Item; | ||
} | ||
|
||
final class StringTablePair | ||
{ | ||
public function __construct( | ||
public readonly string $string, | ||
public readonly int $id, | ||
) {} | ||
} | ||
|
||
final class StringTableSequence implements Sequence<StringTablePair> | ||
{ | ||
private array $strings; | ||
|
||
public function __construct(StringTable $string_table) { | ||
$this->strings = $string_table->to_assoc_array(); | ||
} | ||
|
||
function next(): ?StringTablePair | ||
{ | ||
$key = \array_key_first($this->strings); | ||
if (!isset($key)) { | ||
return null; | ||
} | ||
$value = \array_shift($this->strings); | ||
return new StringTablePair($key, $value); | ||
} | ||
|
||
/** | ||
* @param callable(Item, Item): Item $f | ||
* @return ?Item | ||
*/ | ||
function reduce(callable $f): ?StringTablePair | ||
{ | ||
$reduction = $this->next(); | ||
if (!isset($reduction)) { | ||
return null; | ||
} | ||
|
||
while (($next = $this->next()) !== null) { | ||
$reduction = $f($reduction, $next); | ||
} | ||
return $reduction; | ||
} | ||
} | ||
|
||
final class StringTable | ||
{ | ||
private array $strings = ["" => 0]; | ||
|
||
public function __construct() {} | ||
|
||
public function offsetGet(string $offset): int | ||
{ | ||
return $this->strings[$offset] ?? throw new \Exception(); | ||
} | ||
|
||
public function offsetExists(string $offset): bool | ||
{ | ||
return \isset($this->strings[$offset]); | ||
} | ||
|
||
public function intern(string $str): int | ||
{ | ||
return $this->strings[$str] | ||
?? ($this->strings[$str] = \count($this->strings)); | ||
} | ||
|
||
public function to_sequence(): StringTableSequence | ||
{ | ||
return new StringTableSequence($this); | ||
} | ||
|
||
public function to_assoc_array(): array { | ||
return $this->strings; | ||
} | ||
} | ||
|
||
?> | ||
--EXPECTF-- | ||
Fatal error: Generic type cannot be part of a union type in %s on line %d | ||
Comment on lines
+99
to
+100
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When I saw my example in here, I got excited that you added basic union support. Nope! 😆 One day 🙏🏻 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As I said to Bob I really want to keep it as small as possible as it's already hurting my brain a bit! But this should be a rather easy limitation to lift :) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
--TEST-- | ||
Implicit interface inheritance with different bound types | ||
--FILE-- | ||
<?php | ||
|
||
interface I1<T> { | ||
public function foo(T $param): T; | ||
} | ||
|
||
interface I2<T> extends I1<T> { | ||
public function bar(int $o, T $param): T; | ||
} | ||
|
||
class C implements I2<float>, I1<string> { | ||
public function foo(float $param): float {} | ||
public function bar(int $o, float $param): float {} | ||
} | ||
|
||
?> | ||
--EXPECTF-- | ||
Fatal error: Bound type T for interface I1 implemented explicitly in C with type string must match the implicitly bound type float from interface I2 in %s on line %d |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
--TEST-- | ||
Implicit interface inheritance with different bound types 2 | ||
--FILE-- | ||
<?php | ||
|
||
interface I1<T> { | ||
public function foo(T $param): T; | ||
} | ||
|
||
interface I2<T> extends I1<T> { | ||
public function bar(int $o, T $param): T; | ||
} | ||
|
||
class C implements I1<string>, I2<float> { | ||
public function foo(string $param): string {} | ||
public function bar(int $o, float $param): float {} | ||
} | ||
|
||
?> | ||
--EXPECTF-- | ||
Fatal error: Bound type T for interface I1 implemented explicitly in C with type string must match the implicitly bound type float from interface I2 in %s on line %d |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
--TEST-- | ||
Implicit interface inheritance with different bound types 3 | ||
--FILE-- | ||
<?php | ||
|
||
interface I1<T> { | ||
public function foo(T $param): T; | ||
} | ||
|
||
interface I2 extends I1<float> { | ||
public function bar(int $o, float $param): float; | ||
} | ||
|
||
class C implements I2, I1<string> { | ||
public function foo(float $param): float {} | ||
public function bar(int $o, float $param): float {} | ||
} | ||
|
||
?> | ||
--EXPECTF-- | ||
Fatal error: Bound type T for interface I1 implemented explicitly in C with type string must match the implicitly bound type float from interface I2 in %s on line %d |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
--TEST-- | ||
Implicit interface inheritance with different bound types 4 | ||
--FILE-- | ||
<?php | ||
|
||
interface I1<T> { | ||
public function foo(T $param): T; | ||
} | ||
|
||
interface I2 extends I1<float> { | ||
public function bar(int $o, float $param): float; | ||
} | ||
|
||
class C implements I1<string>, I2 { | ||
public function foo(string $param): string {} | ||
public function bar(int $o, float $param): float {} | ||
} | ||
|
||
?> | ||
--EXPECTF-- | ||
Fatal error: Bound type T for interface I1 implemented explicitly in C with type string must match the implicitly bound type float from interface I2 in %s on line %d |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
--TEST-- | ||
Implicit interface inheritance missing bound types | ||
--FILE-- | ||
<?php | ||
|
||
interface I1<T> { | ||
public function foo(T $param): T; | ||
} | ||
|
||
interface I2<T> extends I1<T> { | ||
public function bar(int $o, T $param): T; | ||
} | ||
|
||
class C implements I2<float>, I1 { | ||
public function foo(float $param): float {} | ||
public function bar(int $o, float $param): float {} | ||
} | ||
|
||
?> | ||
--EXPECTF-- | ||
Fatal error: Interface I1 expects 1 generic parameters, 0 given in %s on line %d |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
--TEST-- | ||
Implicit interface inheritance missing bound types 2 | ||
--FILE-- | ||
<?php | ||
|
||
interface I1<T> { | ||
public function foo(T $param): T; | ||
} | ||
|
||
interface I2<T> extends I1<T> { | ||
public function bar(int $o, T $param): T; | ||
} | ||
|
||
class C implements I1, I2<float> { | ||
public function foo(float $param): float {} | ||
public function bar(int $o, float $param): float {} | ||
} | ||
|
||
?> | ||
--EXPECTF-- | ||
Fatal error: Interface I1 expects 1 generic parameters, 0 given in %s on line %d |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
--TEST-- | ||
Implicit interface inheritance missing bound types 3 | ||
--FILE-- | ||
<?php | ||
|
||
interface I1<T> { | ||
public function foo(T $param): T; | ||
} | ||
|
||
interface I2 extends I1<string> { | ||
public function bar(int $o, string $param): string; | ||
} | ||
|
||
class C implements I1, I2 { | ||
public function foo(string $param): string {} | ||
public function bar(int $o, string $param): string {} | ||
} | ||
|
||
?> | ||
--EXPECTF-- | ||
Fatal error: Interface I1 expects 1 generic parameters, 0 given in %s on line %d |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
--TEST-- | ||
Implicit interface inheritance missing bound types 4 | ||
--FILE-- | ||
<?php | ||
|
||
interface I1<T> { | ||
public function foo(T $param): T; | ||
} | ||
|
||
interface I2 extends I1<string> { | ||
public function bar(int $o, string $param): string; | ||
} | ||
|
||
class C implements I2, I1 { | ||
public function foo(string $param): string {} | ||
public function bar(int $o, string $param): string {} | ||
} | ||
|
||
?> | ||
--EXPECTF-- | ||
Fatal error: Interface I1 expects 1 generic parameters, 0 given in %s on line %d |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
--TEST-- | ||
Implicit interface inheritance with same bound types | ||
--FILE-- | ||
<?php | ||
|
||
interface I1<T> { | ||
public function foo(T $param): T; | ||
} | ||
|
||
interface I2<T> extends I1<T> { | ||
public function bar(int $o, T $param): T; | ||
} | ||
|
||
class C implements I2<string>, I1<string> { | ||
public function foo(string $param): string {} | ||
public function bar(int $o, string $param): string {} | ||
} | ||
|
||
?> | ||
DONE | ||
--EXPECT-- | ||
DONE |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
--TEST-- | ||
Implicit interface inheritance with same bound types 2 | ||
--FILE-- | ||
<?php | ||
|
||
interface I1<T> { | ||
public function foo(T $param): T; | ||
} | ||
|
||
interface I2<T> extends I1<T> { | ||
public function bar(int $o, T $param): T; | ||
} | ||
|
||
class C implements I1<string>, I2<string> { | ||
public function foo(string $param): string {} | ||
public function bar(int $o, string $param): string {} | ||
} | ||
|
||
?> | ||
DONE | ||
--EXPECT-- | ||
DONE |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
--TEST-- | ||
Abstract generic type with a constraint | ||
--FILE-- | ||
<?php | ||
|
||
interface I<T : int|string> { | ||
public function foo(T $param): T; | ||
} | ||
|
||
class CS implements I<string> { | ||
public function foo(string $param): string { | ||
return $param . '!'; | ||
} | ||
} | ||
|
||
class CI implements I<int> { | ||
public function foo(int $param): int { | ||
return $param + 42; | ||
} | ||
} | ||
|
||
$cs = new CS(); | ||
var_dump($cs->foo("Hello")); | ||
|
||
$ci = new CI(); | ||
var_dump($ci->foo(5)); | ||
|
||
?> | ||
--EXPECT-- | ||
string(6) "Hello!" | ||
int(47) |