rfc:nullsafe_calls

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
Next revisionBoth sides next revision
rfc:nullsafe_calls [2014/10/22 23:42] jwatzmanrfc:nullsafe_calls [2014/12/09 23:08] jwatzman
Line 1: Line 1:
 ====== PHP RFC: Nullsafe Calls ====== ====== PHP RFC: Nullsafe Calls ======
-  * Version: 0.+  * Version: 1.0 
-  * Date: 2014-10-22+  * Date: 2014-12-09
   * Author: Josh Watzman (jwatzman@fb.com), Drew Paroski   * Author: Josh Watzman (jwatzman@fb.com), Drew Paroski
-  * Status: Draft+  * Status: Under Discussion
   * First Published at: https://wiki.php.net/rfc/nullsafe_calls   * First Published at: https://wiki.php.net/rfc/nullsafe_calls
  
Line 42: Line 42:
 } }
 </PHP> </PHP>
 +
 +==== Short Circuit ====
 +If ''$obj'' is null, when ''$obj<nowiki>?-></nowiki>foo(..)'' executes, the arguments will still be evaluated. In other words, ''<nowiki>?-></nowiki>'' does **not** have short circuit semantics when evaluating arguments.
 +
 +This is done because it parallels what the ''<nowiki>-></nowiki>'' operator does. The arguments are evaluated whether or not the function being called actually consumes them.  Furthermore, ''<nowiki>?-></nowiki>'' is effectively an error suppression/propagation mechanism. This means that its usage should not affect the way arguments are evaluated; doing anything else would be very confusing for the programmer.
 +
 +It's worth noting that this point has deep implications for the implementation, which must at least begin to actually execute the call opcode, so that arguments can be evaluated. It cannot simply be implemented as a syntactic transform into a ternary or similar!
  
 ==== Implications ==== ==== Implications ====
Line 48: Line 55:
   * If ''$obj'' is an object whose class does **not** define a method "foo", then ''$obj<nowiki>?-></nowiki>foo(..)'' will still raise a fatal error.   * If ''$obj'' is an object whose class does **not** define a method "foo", then ''$obj<nowiki>?-></nowiki>foo(..)'' will still raise a fatal error.
   *  If ''$obj'' is anything other than null or object, then ''$obj<nowiki>?-></nowiki>foo(..)'' will still raise a fatal error.   *  If ''$obj'' is anything other than null or object, then ''$obj<nowiki>?-></nowiki>foo(..)'' will still raise a fatal error.
-  * If ''$obj'' is null, when ''$obj<nowiki>?-></nowiki>foo(..)'' executes, the arguments will still be evaluated. In other words, ''<nowiki>?-></nowiki>'' does **not** have short circuit semantics when evaluating arguments. 
  
 ==== Prior Art ==== ==== Prior Art ====
   * C#, CoffeeScript, and Groovy all have a "safe navigation operator" which was the original inspiration for this feature.   * C#, CoffeeScript, and Groovy all have a "safe navigation operator" which was the original inspiration for this feature.
 +  * Haskell has the "maybe monad", which syntactically looks quite different but morally provides a similar mechanism to propagate any failure in a computation forward to the end of the computation.
   * Hack has already implemented a proposal identical to this one.   * Hack has already implemented a proposal identical to this one.
  
 ===== Backward Incompatible Changes ===== ===== Backward Incompatible Changes =====
-What breaks, and what is the justification for it?+Due to an implementation detail, this decreases the maximum number of arguments a function can be called with from ''2^32'' to ''2^31'', and adds an error when that limit is reached. (The engine would previously just wrap around, to potentially disastrous consequences.) 
 + 
 +This is just a technicality... all of my attempts to actually hit that limit put my machine into swapdeath long before I got close :-P 
 + 
 +See also "RFC Impact To Existing Extensions" below.
  
 ===== Proposed PHP Version(s) ===== ===== Proposed PHP Version(s) =====
-This is proposed for the next major version of PHP, currently PHP 7.+PHP7.
  
 ===== RFC Impact ===== ===== RFC Impact =====
-==== To SAPIs ==== 
-Describe the impact to CLI, Development web server, embedded PHP etc. 
- 
 ==== To Existing Extensions ==== ==== To Existing Extensions ====
-Will existing extensions be affected?+Extensions have access to an opline's ''extended_value'', and the current implementation re-uses a single bit at the top of it for a new purpose. This is a backwards compatibility break for extensions which read the ''extended_value'' out of the "begin fcall" opcode -- though arguably extensions like RunKit which do this probably shouldn'be anyways ;)
  
-==== To Opcache ==== +If this impact is deemed too muchthere are certainly other implementation options, which I think are less attractiveWe could add a new ''OP_DATA'' opcode after the begin fcall, but that seems like dramatic overkill for storing a single bit.
-It is necessary to develop RFC's with opcache in mindsince opcache is a core extension distributed with PHP. +
- +
-Please explain how you have verified your RFC's compatibility with opcache.+
  
 ==== New Constants ==== ==== New Constants ====
-Describe any new constants so they can be accurately and comprehensively explained in the PHP documentation. +Nothing accessible from outside the internals of the engine.
- +
-==== php.ini Defaults ==== +
-If there are any php.ini settings then list: +
-  * hardcoded default values +
-  * php.ini-development values +
-  * php.ini-production values+
  
 ===== Open Issues ===== ===== Open Issues =====
Line 85: Line 84:
  
 ===== Unaffected PHP Functionality ===== ===== Unaffected PHP Functionality =====
-This RFC does not change any existing PHP behavior, including the ''<nowiki>-></nowiki>'' operator, the ''??'' operator, or other error suppression mechanisms.+This RFC does not change any existing PHP behavior, including the ''<nowiki>-></nowiki>'' operator, the ''??'' operator, the ''@'' operator, or other error suppression mechanisms.
  
 ===== Future Scope ===== ===== Future Scope =====
Line 94: Line 93:
  
 ===== Patches and Tests ===== ===== Patches and Tests =====
-  * php-src: I have a scratch branch at https://github.com/jwatzman/php-src/compare/nullsafe-scratch?expand=1 with a working implementation. As the RFC progresses, I plan to squash that down into a nicer PR for review+  * php-src: I have a branch at https://github.com/jwatzman/php-src/compare/nullsafe-prototype?expand=1 with a working implementation. Includes tests copied from HHVM's implementation
-  * PHP spec: nothing yet, but this shouldn't take too long.+  * PHP spec: not yet, but will do if the RFC is accepted.
   * PHP docs: import Hack's documentation when they add it: https://github.com/hhvm/hack-hhvm-docs/issues/360 (that task will get completed well before this RFC is voted on, accepted, merged, etc)   * PHP docs: import Hack's documentation when they add it: https://github.com/hhvm/hack-hhvm-docs/issues/360 (that task will get completed well before this RFC is voted on, accepted, merged, etc)
  
rfc/nullsafe_calls.txt · Last modified: 2020/08/03 10:09 by ilutov