rfc:nullsafe_calls
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revisionNext revisionBoth sides next revision | ||
rfc:nullsafe_calls [2014/10/22 22:04] – created jwatzman | rfc:nullsafe_calls [2014/12/09 22:12] – jwatzman | ||
---|---|---|---|
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 | ||
* First Published at: https:// | * First Published at: https:// | ||
- | This is a suggested template for PHP Request for Comments (RFCs). Change this template | + | ===== Introduction ===== |
- | Read https:// | + | This RFC proposes |
+ | ===== Proposal ===== | ||
+ | A very common pattern is to have some a computation consisting of a series of method calls, any one of which can return null. If any of them do return null, the entire computation should fail. Right now, writing something like this in PHP requires a bunch of boilerplate checks after each call. For example: | ||
- | Quoting [[http:// | + | < |
+ | function f($o) { | ||
+ | $o2 = $o-> | ||
+ | if ($o2 === null) { | ||
+ | return null; | ||
+ | } | ||
- | > PHP is and should remain: | + | $o3 = $o2->mayFail2(); |
- | > 1) a pragmatic web-focused language | + | if ($o3 === null) { |
- | > 2) a loosely typed language | + | |
- | > 3) a language which caters to the skill-levels and platforms of a wide range of users | + | } |
- | Your RFC should move PHP forward following his vision. As [[http:// | + | $o4 = $o3-> |
- | large chunk of our userbase, and not something that could be useful in some | + | |
- | extremely specialized edge cases [...] Make sure you think about the full context, the huge audience out there, the consequences of making the learning curve steeper with | + | |
- | every new feature, and the scope of the goodness that those new features bring." | + | } |
- | ===== Introduction ===== | + | return $o4-> |
- | The elevator pitch for the RFC. The first paragraph in this section will be slightly larger to give it emphasis; please write a good introduction. | + | } |
+ | </ | ||
- | ===== Proposal ===== | + | This certainly works, but it's a lot of boilerplate for a fairly common pattern, |
- | All the features | + | |
- | To [[http:// | + | This means that calls using this new operator can be chained together. For example, the code above, rewritten with the new operator: |
- | for inclusion in one of the world' | + | |
- | Remember that the RFC contents | + | < |
+ | 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) ===== | ||
Line 40: | Line 72: | ||
===== 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 have access to an opline' |
- | ==== To Opcache ==== | + | If this impact |
- | It is necessary to develop RFC's with opcache in mind, since 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 |
- | + | ||
- | ==== 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 64: | Line 84: | ||
===== Unaffected PHP Functionality ===== | ===== Unaffected PHP Functionality ===== | ||
- | List existing | + | This RFC does not change any existing PHP behavior, including |
- | + | ||
- | This helps avoid any ambiguity, shows that you have thought deeply about the RFC's impact, and helps reduces mail list noise. | + | |
===== 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 ===== | ||
- | Include these so readers know where you are heading | + | This is pretty clearly a core language change |
- | + | ||
- | State whether this project | + | |
===== Patches and Tests ===== | ===== Patches and Tests ===== | ||
- | Prototype: https:// | + | * php-src: I have a branch at https:// |
+ | * 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 86: | Line 104: | ||
===== References ===== | ===== References ===== | ||
- | Links to external references, discussions or RFCs | + | HHVM's implementation for the Hack language is at https:// |
===== 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