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/01/29 10:06] – mindplay | rfc:generics [2016/04/25 15:19] – mindplay | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== PHP RFC: Generic Types and Functions ====== | ====== PHP RFC: Generic Types and Functions ====== | ||
- | * Version: 0.3.2 | + | * Version: 0.4.0 |
* Date: 2016-01-06 | * Date: 2016-01-06 | ||
* Author: Ben Scholzen ' | * Author: Ben Scholzen ' | ||
- | * Status: | + | * Status: |
* First Published at: http:// | * First Published at: http:// | ||
===== 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 '' |
Generic type relationships are already possible in PHP, as it is in most dynamic languages - but such type relationships presently cannot be declared or checked, and, at this time, can't even be documented, e.g. using php-doc tags. | Generic type relationships are already possible in PHP, as it is in most dynamic languages - but such type relationships presently cannot be declared or checked, and, at this time, can't even be documented, e.g. using php-doc tags. | ||
Line 35: | Line 35: | ||
A type parameter may include a default type-hint, which will be applied in the absence of a given type-hint, indicated by an '' | A type parameter may include a default type-hint, which will be applied in the absence of a given type-hint, indicated by an '' | ||
- | Within the scope of a given generic type declaration, | + | Within the scope of a given generic type declaration, |
+ | |||
+ | The use of class-level | ||
The following demonstrates the proposed syntax for a generic class: | The following demonstrates the proposed syntax for a generic class: | ||
Line 115: | 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 144: | 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 219: | Line 253: | ||
class Box< | class Box< | ||
{ | { | ||
- | use Box<T>; | + | use Container<T>; |
} | } | ||
Line 293: | 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 328: | Line 378: | ||
As demonstrated by the last two examples, type-checking also works on abstract types - that is, an instance of '' | As demonstrated by the last two examples, type-checking also works on abstract types - that is, an instance of '' | ||
- | ==== Autoloading ==== | + | === Bounded Polymorphism |
- | When autoloading is triggered e.g. by a '' | + | TODO: decide whether or not [[https:// |
- | In other words, a statement like '' | + | === Multiple Constraints === |
- | ==== Generic Arrays ==== | + | TODO: decide whether |
- | + | ||
- | The built-in array type comes with two generic counterparts - one with a value type, and one with key and value types. | + | |
- | + | ||
- | The '' | + | |
- | + | ||
- | < | + | |
- | $counts = array< | + | |
- | $counts[" | + | |
- | + | ||
- | $versions = array< | + | |
- | $versions[" | + | |
- | </ | + | |
- | + | ||
- | Violating the key or value type of a type-checked array will trigger a '' | + | |
- | + | ||
- | The actual type-checks or type-conversions performed will depend on the '' | + | |
- | + | ||
- | Note that there is no generic form of the short array syntax - generic arrays can only be created explicitly by using the '' | + | |
- | + | ||
- | === Array Type-casting === | + | |
- | + | ||
- | Generic arrays can be explicitly [[http:// | + | |
<code php> | <code php> | ||
- | $array = [1,2,3]; | + | class A<T> where T is T1, T is T2 { |
- | + | // ... | |
- | $numbers = (array< | + | } |
</ | </ | ||
- | In this example, | + | This may relate to the [[https:// |
- | Casting to a less general index or element type than that of the source array, results in creation of a new array, and keys/ | + | ==== Autoloading ==== |
- | If an array is cast to a more general (or identical) index and element | + | When autoloading |
- | The following results in an implicit type-cast: | + | In other words, a statement like '' |
- | + | ||
- | <code php> | + | |
- | function tally(array< | + | |
- | // ... | + | |
- | } | + | |
- | + | ||
- | tally([7,8,9]); | + | |
- | </ | + | |
- | + | ||
- | In this case, the implicit type-cast is successful. In a case where the conversion fails, a '' | + | |
==== Reflection ==== | ==== Reflection ==== | ||
Line 388: | 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 429: | 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 ===== | ||
+ | |||
+ | Generic arrays (and related functions) were previously part of this RFC, but have been moved to a dedicated [[https:// | ||
===== References ===== | ===== References ===== | ||
https:// | https:// |
rfc/generics.txt · Last modified: 2018/06/02 17:33 by mindplay