rfc:nullsafe_calls
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionLast revisionBoth sides next revision | ||
rfc:nullsafe_calls [2014/10/22 23:11] – jwatzman | rfc:nullsafe_calls [2017/09/22 13:28] – external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== PHP RFC: Nullsafe Calls ====== | ====== PHP RFC: Nullsafe Calls ====== | ||
- | * Version: | + | * Version: 1.0 |
- | * Date: 2014-10-22 | + | * Date: 2014-12-09 |
* Author: Josh Watzman (jwatzman@fb.com), | * Author: Josh Watzman (jwatzman@fb.com), | ||
* Status: Draft | * Status: Draft | ||
Line 7: | Line 7: | ||
===== Introduction ===== | ===== Introduction ===== | ||
+ | **The RFC has been returned to draft stage after discussion on internals in order to figure out how to deal with short circuiting. See the "open issues" | ||
+ | |||
This RFC proposes a new operator, the " | This RFC proposes a new operator, the " | ||
Line 32: | Line 34: | ||
} | } | ||
</ | </ | ||
+ | |||
+ | This certainly works, but it's a lot of boilerplate for a fairly common pattern, and it's also a lot of explicit checks that the runtime must do. Instead, a new operator is added to the language: ''< | ||
+ | |||
+ | This means that calls using this new operator can be chained together. For example, the code above, rewritten with the new operator: | ||
+ | |||
+ | <PHP> | ||
+ | function f($o) { | ||
+ | return $o? | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ==== Short Circuit ==== | ||
+ | If '' | ||
+ | |||
+ | This is done because it parallels what the ''< | ||
+ | |||
+ | It's worth noting that this point has deep implications for the implementation, | ||
+ | |||
+ | ==== Implications ==== | ||
+ | For the purpose of clarity, some implications of the above definition; all of these stem from consistency between ''< | ||
+ | |||
+ | * If '' | ||
+ | * If '' | ||
+ | |||
+ | ==== Prior Art ==== | ||
+ | * C#, CoffeeScript, | ||
+ | * Haskell has the "maybe monad", | ||
+ | * 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 '' |
+ | |||
+ | 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" | ||
===== 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 Opcache ==== | + | |
- | It is necessary | + | |
- | Please explain how you have verified your RFC's compatibility with opcache. | + | If this impact is deemed too much, there are certainly other implementation options, which I think are less attractive. We could add a new '' |
==== New Constants ==== | ==== New Constants ==== | ||
- | Describe any new constants so they can be accurately and comprehensively explained in the PHP documentation. | + | Nothing accessible from outside |
- | + | ||
- | ==== 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 ===== | ||
Make sure there are no open issues when the vote starts! | Make sure there are no open issues when the vote starts! | ||
+ | |||
+ | ==== Short Circuit ==== | ||
+ | The behavior for (not) short circuiting argued for above is not clearly the right behavior. There are actually at least //three// meaningful possibilities here. I'm currently investigating implementation feasibility in both PHP7 and in HHVM, as well as generally thinking about what the right thing to do is, and will bring the discussion back up on internals once I've got my thoughts together better. | ||
+ | |||
+ | As a quick preview, the three options can be seen as to how to desugar the following code. I'm not going to argue for or against any of them yet, just show what the range of possibilities are. (I also haven' | ||
+ | |||
+ | <PHP> | ||
+ | $r = $x? | ||
+ | </ | ||
+ | |||
+ | === Option 1: no short circuit === | ||
+ | Arguments are evaluated even if we are doing the nullsafe call on null. | ||
+ | |||
+ | <PHP> | ||
+ | $_tmp1 = f(); | ||
+ | $_tmp2 = g(); | ||
+ | $_tmp3 = $x === null ? null : $x-> | ||
+ | $r = $_tmp3-> | ||
+ | </ | ||
+ | |||
+ | === Option 2: one-level short circuit === | ||
+ | Arguments are not evaluated if we are doing the nullsafe call on null. The nullsafe behavior only applies to the single function call where the nullsafe operator is used. | ||
+ | |||
+ | <PHP> | ||
+ | $_tmp1 = $x === null ? null : $x-> | ||
+ | $r = $_tmp1-> | ||
+ | </ | ||
+ | |||
+ | === Option 3: full short circuit === | ||
+ | Arguments are not evaluated if we are doing the nullsafe call on null. The nullsafe behavior applies to all calls chained after the nullsafe operator. | ||
+ | |||
+ | <PHP> | ||
+ | $r = $x === null ? null : $x-> | ||
+ | </ | ||
===== Unaffected PHP Functionality ===== | ===== Unaffected PHP Functionality ===== | ||
- | This RFC does not change any existing PHP behavior, including the ''< | + | This RFC does not change any existing PHP behavior, including the ''< |
===== Future Scope ===== | ===== Future Scope ===== | ||
- | This sections details areas where the feature might be improved in future, but that are not currently proposed in this RFC. | + | The ''< |
===== Proposed Voting Choices ===== | ===== Proposed Voting Choices ===== | ||
Line 73: | Line 129: | ||
===== Patches and Tests ===== | ===== Patches and Tests ===== | ||
- | I have a scratch | + | * php-src: |
+ | * PHP spec: not yet, but will do if the RFC is accepted. | ||
+ | * PHP docs: import Hack's documentation when they add it: https:// | ||
===== Implementation ===== | ===== Implementation ===== | ||
Line 82: | Line 140: | ||
===== References ===== | ===== References ===== | ||
- | HHVM's implementation | + | HHVM's implementation |
===== Rejected Features ===== | ===== Rejected Features ===== | ||
Keep this updated with features that were discussed on the mail lists. | Keep this updated with features that were discussed on the mail lists. |
rfc/nullsafe_calls.txt · Last modified: 2020/08/03 10:09 by ilutov