rfc:generics
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
rfc:generics [2016/02/28 10:11] – clarification; change status to Draft mindplay | rfc:generics [2016/04/25 15:19] – mindplay | ||
---|---|---|---|
Line 9: | Line 9: | ||
===== Introduction ===== | ===== Introduction ===== | ||
- | This RCF proposes the addition of generic types and functions to PHP. | + | This RFC proposes the addition of generic types and functions to PHP. |
Generics enable developers to create a whole family of declarations using a single generic declaration - for example, a generic collection-type declaration '' | Generics enable developers to create a whole family of declarations using a single generic declaration - for example, a generic collection-type declaration '' | ||
Line 117: | Line 117: | ||
In the second example, the '' | In the second example, the '' | ||
+ | |||
+ | === Nested Type Arguments === | ||
+ | |||
+ | Generic classes may be instantiated and generic functions/ | ||
+ | |||
+ | <code php> | ||
+ | class Container< | ||
+ | { | ||
+ | private $content; | ||
+ | | ||
+ | public function getContent(): | ||
+ | { | ||
+ | return $this-> | ||
+ | } | ||
+ | | ||
+ | public function setContent(ContentType $content): void | ||
+ | { | ||
+ | $this-> | ||
+ | } | ||
+ | } | ||
+ | |||
+ | $container = new Container< | ||
+ | |||
+ | $container-> | ||
+ | var_dump($container-> | ||
+ | |||
+ | $container-> | ||
+ | </ | ||
+ | |||
+ | In this example, the '' | ||
+ | |||
+ | In the second example, the '' | ||
=== Upper Bounds === | === Upper Bounds === | ||
Line 146: | Line 178: | ||
Any valid PHP type-hint may be used as an upper bound, including simple types like '' | Any valid PHP type-hint may be used as an upper bound, including simple types like '' | ||
- | Note that the choice of the keyword '' | + | Note that the choice of the keyword '' |
== Bounds Checking == | == Bounds Checking == | ||
Line 221: | Line 253: | ||
class Box< | class Box< | ||
{ | { | ||
- | use Box<T>; | + | use Container<T>; |
} | } | ||
Line 295: | Line 327: | ||
The same applies when overriding constructors and static methods. | The same applies when overriding constructors and static methods. | ||
+ | |||
+ | ==== Generic Constructors ==== | ||
+ | |||
+ | Constructors may accept arbitrary type-arguments, | ||
+ | |||
+ | <code php> | ||
+ | class Hello< | ||
+ | { | ||
+ | public function __construct< | ||
+ | { | ||
+ | // ... | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | In other words, the constructor may accept more type-arguments than those affecting the type. | ||
==== Generic Closures ==== | ==== Generic Closures ==== | ||
- | TODO describe '' | + | TODO describe '' |
==== Type Checking ==== | ==== Type Checking ==== | ||
Line 333: | Line 381: | ||
TODO: decide whether or not [[https:// | TODO: decide whether or not [[https:// | ||
+ | |||
+ | === Multiple Constraints === | ||
+ | |||
+ | TODO: decide whether or not multiple constraints should be supported, e.g. with a Java-like syntax: | ||
+ | |||
+ | <code php> | ||
+ | class A<T> where T is T1, T is T2 { | ||
+ | // ... | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | This may relate to the [[https:// | ||
==== Autoloading ==== | ==== Autoloading ==== | ||
Line 346: | Line 406: | ||
This RFC calls for the following changes and additions to the reflection API: | This RFC calls for the following changes and additions to the reflection API: | ||
- | TODO | + | TODO (some [[https:// |
=== Reification === | === Reification === | ||
Line 387: | Line 447: | ||
No patch has been written for this yet. As I'm not a C-coder myself, I encourage others to write a patch based on this proposal. | No patch has been written for this yet. As I'm not a C-coder myself, I encourage others to write a patch based on this proposal. | ||
+ | |||
+ | Some [[https:// | ||
+ | |||
+ | The same fork also contains some experimental parser enhancements written by Dominic Grostate. | ||
===== Related RFCs ===== | ===== Related RFCs ===== |
rfc/generics.txt · Last modified: 2018/06/02 17:33 by mindplay