rfc:sum_types
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
rfc:sum_types [2015/02/16 16:35] – |false levim | rfc:sum_types [2015/03/11 15:47] (current) – Deleted levim | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== PHP RFC: Sum Types ====== | ||
- | * Version: 0.1 | ||
- | * Date: 2015-02-14 | ||
- | * Author: Levi Morrison < | ||
- | * Status: Draft | ||
- | * First Published at: http:// | ||
- | ===== Introduction ===== | ||
- | In PHP 5.1 the ability to provide explicit types for parameters was added to PHP. It supported classes, interfaces and arrays. In PHP 5.4 the '' | ||
- | |||
- | All of these requests and discussions suggest that the ability to accept multiple types for a parameter or return type could be useful. This RFC is an investigation into the feasibility of this idea and to flesh out the impact it would have on other parts of PHP. | ||
- | |||
- | ===== Proposal ===== | ||
- | This RFC proposes the ability to define multiple types for a parameter or return type and calls them "sum types" | ||
- | |||
- | <PHP> | ||
- | function (array|Traversable $in) { | ||
- | foreach ($in as $value) { | ||
- | echo $value, PHP_EOL; | ||
- | } | ||
- | } | ||
- | </ | ||
- | |||
- | Assuming that we allow a type of '' | ||
- | |||
- | Peter Cowburn (salathe) has suggested that being able to explicitly use '' | ||
- | |||
- | < | ||
- | strpos ( string $haystack , mixed $needle [, int $offset = 0 ] ): mixed | ||
- | // to | ||
- | strpos ( string $haystack , mixed $needle [, int $offset = 0 ] ): int|false</ | ||
- | |||
- | ===== Proposed PHP Version(s) ===== | ||
- | It is not yet known if this requires backwards compatibility breaks. It could theoretically target PHP 7.NEXT but possibly should wait until PHP 8 (or whatever we call it). | ||
- | |||
- | ===== RFC Impact To Opcache ===== | ||
- | There is not yet an implementation, | ||
- | |||
- | ===== Open Issues ===== | ||
- | ==== Weak Scalar Types ==== | ||
- | If PHP adds explicit weak scalar types there is a question of how things will get converted in some situations. As an example, if we have a sum type of '' | ||
- | |||
- | <PHP> | ||
- | function f(int|float $number) { | ||
- | return $number * 2; | ||
- | } | ||
- | f(" | ||
- | </ | ||
- | |||
- | Would it be converted to '' | ||
- | |||
- | This issue would be avoided if a parameter type exactly matches the input type or if PHP has strict scalar types. | ||
- | |||
- | ==== Long Type Names ==== | ||
- | Since you can create a chain of types the names can get quite lengthy. Even the fairly short sum type of '' | ||
- | |||
- | ==== Reflection ==== | ||
- | There isn't any part of our current Reflection extension that can deal with sum types and I have not yet given it any thought. | ||
- | ===== Future Scope ===== | ||
- | This sections details areas where the feature might be improved in future, but that are not currently proposed in this RFC. | ||
- | |||
- | ===== Proposed Voting Choices ===== | ||
- | This RFC requires that two-thirds of voters vote in favor of the RFC to pass. | ||
- | |||
- | ===== Patches and Tests ===== | ||
- | Presently no patch. | ||
- | |||
- | ===== References ===== | ||
- | Links to external references, discussions or RFCs | ||
- | |||
- | ===== Rejected Features ===== | ||
- | Keep this updated with features that were discussed on the mail lists. |
rfc/sum_types.1424104512.txt.gz · Last modified: 2017/09/22 13:28 (external edit)