rfc:nullable-casting

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:nullable-casting [2019/03/26 07:51] – reorder, reword guilliamxavierrfc:nullable-casting [2019/04/21 09:03] (current) – Add Discussion section guilliamxavier
Line 4: Line 4:
   * Date: 2019-03-17   * Date: 2019-03-17
   * Author: David Rodrigues (david.proweb@gmail.com), Guilliam Xavier (guilliam.xavier@gmail.com)   * Author: David Rodrigues (david.proweb@gmail.com), Guilliam Xavier (guilliam.xavier@gmail.com)
-  * Status: Draft+  * Status: Under Discussion
   * First Published at: http://wiki.php.net/rfc/nullable-casting   * First Published at: http://wiki.php.net/rfc/nullable-casting
  
Line 15: Line 15:
 ===== Motivating Example ===== ===== Motivating Example =====
  
-In strict type-checking mode (<php><?php declare(strict_types=1);</php>), given two functions like the following:+In strict type-checking mode (<php><?php declare(strict_types=1);</php>), given two functions like the following (disclaimer: dummy implementation):
  
 <PHP> <PHP>
Line 167: Line 167:
  
   * **Accept nullable casting?**: Simple vote (Yes / No), requiring a 2/3 majority to pass.   * **Accept nullable casting?**: Simple vote (Yes / No), requiring a 2/3 majority to pass.
-    * **Additionally accept nullable settype()?**: Simple vote (Yes / No), requiring a 2/3 majority to pass.+    * **Additionally accept nullable settype()?**: Simple vote (Yes / No), also requiring a 2/3 majority to pass.
       * **How to handle settype($x, "?null")?**: Multi-options vote (Allow silently / Allow but Notice / Disallow with specific Warning / Disallow with the generic Warning), the option with more votes will win.       * **How to handle settype($x, "?null")?**: Multi-options vote (Allow silently / Allow but Notice / Disallow with specific Warning / Disallow with the generic Warning), the option with more votes will win.
 +
 +(The voting period would be two weeks)
  
 ===== Patches and Tests ===== ===== Patches and Tests =====
  
   * Working prototype: https://github.com/php/php-src/pull/3764   * Working prototype: https://github.com/php/php-src/pull/3764
 +
 +===== Discussion =====
 +
 +==== "Not 100% needed" ====
 +
 +Current alternatives:
 +  * Use a test (ternary conditional operator or ''if'' statement), possibly with a temporary variable
 +  * Write (and [auto]load) custom casting functions
 +  * Disable (i.e. do not enable) strict typing mode in the concerned file //(not strictly equivalent, e.g. for ''%%"foo"%%'' to ''int'')//
 +
 +==== "A cast where you can't be sure of what you'll get back" ====
 +
 +"I understand the use-case for when you want to pass something to a nullable parameter, but if you think about this cast in isolation, it hardly makes sense."
 +
 +==== "What about e.g. nullable_intval()?" ====
 +
 +But we're missing "arrayval()" and "objectval()"... And we might use short closure ''%%fn($x) => (?int)$x%%''
 +
 +==== Fallible Casting ====
 +
 +One might expect to also have e.g. ''%%(?int)"foo"%%'' and ''%%(?int)""%%'' give ''null'' rather than ''0'', ''(?string)[42]'' give ''null'' rather than ''%%"Array"%%''... and to be able to use ''(?int)$value ?? $default'', ''%%(?string)$_GET["input"] ?? ""%%''...
 +
 +==== Alternative syntax ====
 +
 +E.g. "''(null|int) $x''"
  
 ===== References ===== ===== References =====
  
-  * [[http://php.net/manual/en/language.types.type-juggling.php|PHP Manual: Type Juggling]] +  * PHP Manual: [[http://php.net/manual/en/language.types.type-juggling.php|Type Juggling]][[http://php.net/manual/en/function.settype.php|settype() function]] 
-  * [[http://php.net/manual/en/function.settype.php|PHP Manual: settype() function]] +  * PHP RFC: [[rfc:scalar_type_hints_v5|Scalar Type Declarations]][[rfc:nullable_types|Nullable Types]]
-  * [[rfc:scalar_type_hints_v5|PHP RFC: Scalar Type Declarations]] +
-  * [[rfc:nullable_types|PHP RFC: Nullable Types]]+
   * Initial idea and discussion: https://externals.io/message/102997   * Initial idea and discussion: https://externals.io/message/102997
 +  * Annoucement and discussion: https://externals.io/message/105122
rfc/nullable-casting.1553586699.txt.gz · Last modified: 2019/03/26 07:51 by guilliamxavier