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/04/06 07:45] – Change Status to Under Discussion guilliamxavierrfc:nullable-casting [2019/04/21 09:03] (current) – Add Discussion section guilliamxavier
Line 175: Line 175:
  
   * 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 =====
Line 181: Line 206:
   * PHP RFC: [[rfc:scalar_type_hints_v5|Scalar Type Declarations]], [[rfc:nullable_types|Nullable Types]]   * PHP RFC: [[rfc:scalar_type_hints_v5|Scalar Type Declarations]], [[rfc:nullable_types|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.1554536745.txt.gz · Last modified: 2019/04/06 07:45 by guilliamxavier