rfc:union_types

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
rfc:union_types [2016/06/14 14:35] – Rework grammar in Introduction levimrfc:union_types [2017/09/22 13:28] (current) – external edit 127.0.0.1
Line 3: Line 3:
   * Date: 2015-02-14   * Date: 2015-02-14
   * Author: Levi Morrison <levim@php.net>, Bob Weinand <bobwei9@hotmail.com>   * Author: Levi Morrison <levim@php.net>, Bob Weinand <bobwei9@hotmail.com>
-  * Status: Vote+  * Status: Declined
   * First Published at: http://wiki.php.net/rfc/union_types   * First Published at: http://wiki.php.net/rfc/union_types
  
 ===== Introduction ===== ===== Introduction =====
-Type declarations (function parameter and return types) perform two useful roles:+PHP has [[http://php.net/manual/en/functions.arguments.php#functions.arguments.type-declaration|type declarations]] which can be associated with function parameters or return values. These declarations perform two useful roles:
  
-  * Using types allows the PHP engine to enforce the correct type of variable passed toor returned froma function. +  * They allow the PHP engine to enforce the correct type of variable passed to or returned from a function. 
-  * Using types makes it easy to reason about what types need to be passed toor can be returned from a function. This makes it easier for both humansand static code analysis toolsto determine about whether code is correct or not.+  * They make it easier to reason about what types need to be passed to or can be returned from a function. Both humans and static code analysis tools can use this information to help determine the correctness of the code.
  
 For a lot of functions in PHP each parameter will only be one type. Similarly, for the majority of functions the return value will only ever be of one type. For a lot of functions in PHP each parameter will only be one type. Similarly, for the majority of functions the return value will only ever be of one type.
Line 25: Line 25:
 This RFC seeks to address these limitations. This RFC seeks to address these limitations.
  
-==== Proposal ====+===== Proposal =====
 This RFC proposes the ability to define multiple possible types for parameter and return types. To define a 'union type' a single vertical bar (OR) is placed between types e.g. ''int|bool'' represents the union type of either integer or boolean. For these 'union types' a value passes the type check if the value would pass any one of the types in the union.  This RFC proposes the ability to define multiple possible types for parameter and return types. To define a 'union type' a single vertical bar (OR) is placed between types e.g. ''int|bool'' represents the union type of either integer or boolean. For these 'union types' a value passes the type check if the value would pass any one of the types in the union. 
  
Line 32: Line 32:
 There can be more than two types in the union. There can be more than two types in the union.
  
-=== Parameter type examples ===+==== Parameter type examples ====
 A function that requires either a string or an array is passed to it as the parameter: A function that requires either a string or an array is passed to it as the parameter:
 <PHP> <PHP>
Line 59: Line 59:
 For this example, it is clear to both static analysis tools and humans that passing anything other than a ParameterGenerator object or a string to this function, would be an error.  For this example, it is clear to both static analysis tools and humans that passing anything other than a ParameterGenerator object or a string to this function, would be an error. 
  
-=== Return type example ===+==== Return type example ====
 A userland definition of ''stripos'' function: A userland definition of ''stripos'' function:
  
Line 166: Line 166:
 This RFC requires that two-thirds of voters vote in favor of the RFC to pass. This RFC requires that two-thirds of voters vote in favor of the RFC to pass.
  
-<doodle title="Merge union types" auth="bwoebi" voteType="single" closed="false">+<doodle title="Merge union types" auth="bwoebi" voteType="single" closed="true">
    * Yes    * Yes
    * No    * No
Line 173: Line 173:
 Additionally, there are two 50%+1 votes: Additionally, there are two 50%+1 votes:
  
-<doodle title="Replace ?QuestionMarkNullables by union | null" auth="bwoebi" voteType="single" closed="false">+<doodle title="Replace ?QuestionMarkNullables by union | null" auth="bwoebi" voteType="single" closed="true">
    * Yes    * Yes
    * No    * No
 </doodle> </doodle>
  
-<doodle title="Include true/false types" auth="bwoebi" voteType="single" closed="false">+<doodle title="Include true/false types" auth="bwoebi" voteType="single" closed="true">
    * Yes    * Yes
    * No    * No
rfc/union_types.1465914914.txt.gz · Last modified: 2017/09/22 13:28 (external edit)