rfc:short-functions
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
rfc:short-functions [2021/03/24 16:02] – Add reference to the auto-capture closure RFC. crell | rfc:short-functions [2021/05/31 20:57] – Note the impact of PFA. crell | ||
---|---|---|---|
Line 3: | Line 3: | ||
* Date: 2020-10-20 | * Date: 2020-10-20 | ||
* Author: Larry Garfield (larry@garfieldtech.com) | * Author: Larry Garfield (larry@garfieldtech.com) | ||
- | * Status: | + | * Status: |
- | * First Published at: http:// | + | * First Published at: http:// |
===== Introduction ===== | ===== Introduction ===== | ||
Line 19: | Line 19: | ||
function add(int $a, int $b): int | function add(int $a, int $b): int | ||
{ | { | ||
- | return $a + b; | + | return $a + $b; |
} | } | ||
</ | </ | ||
Line 51: | Line 51: | ||
This RFC is designed to complement the [[rfc: | This RFC is designed to complement the [[rfc: | ||
- | |||
* The '' | * The '' | ||
Line 69: | Line 68: | ||
Expressions are becoming increasingly capable, too. match() expressions and throw expressions in PHP 8.0, plus proposals such as [[rfc: | Expressions are becoming increasingly capable, too. match() expressions and throw expressions in PHP 8.0, plus proposals such as [[rfc: | ||
- | === Pure functions === | + | The trend in PHP in recent years has been toward more compact but still readable syntax that eliminates redundancy. |
+ | |||
+ | ==== Pure functions | ||
Expression functions are also more likely to be pure, and thus avoid mutable state. | Expression functions are also more likely to be pure, and thus avoid mutable state. | ||
Line 83: | Line 84: | ||
However, such code would be readily apparent as using global mutable state, and is easily avoided. | However, such code would be readily apparent as using global mutable state, and is easily avoided. | ||
- | ==== Examples ==== | + | ===== Examples |
Below are some examples of "long form" current code and what the short function equivalent would be. This RFC asserts that the shorter version is more concise and readable. | Below are some examples of "long form" current code and what the short function equivalent would be. This RFC asserts that the shorter version is more concise and readable. | ||
- | === Match functions === | + | ==== Match functions |
A function that encapsulates a match() expression. | A function that encapsulates a match() expression. | ||
Line 116: | Line 117: | ||
</ | </ | ||
- | === Enum methods === | + | ==== Enum methods |
In practice, most enum methods are likely to contain only a match expression, the evaluated value of which should be returned. | In practice, most enum methods are likely to contain only a match expression, the evaluated value of which should be returned. | ||
Line 147: | Line 148: | ||
</ | </ | ||
- | === Getter methods === | + | ==== Getter methods |
- | Many classes consist primarily or almost entirely | + | Many classes consist primarily or almost entirely of methods that either return a property, or some computation off of a property. |
<code php> | <code php> | ||
Line 194: | Line 195: | ||
</ | </ | ||
- | === Functional code === | + | ==== Functional code ==== |
<code php> | <code php> | ||
Line 218: | Line 219: | ||
</ | </ | ||
- | === Conditional methods === | + | ==== Conditional methods |
A common refactoring technique is to take a complex conditional in an if statement and move it to its own method, so it can be given a self-descriptive name. Such methods are naturally single-expression. | A common refactoring technique is to take a complex conditional in an if statement and move it to its own method, so it can be given a self-descriptive name. Such methods are naturally single-expression. | ||
Line 243: | Line 244: | ||
Which is more simple and compact than a full function body. | Which is more simple and compact than a full function body. | ||
- | === Decorating functions in live code === | + | ==== Decorating functions in live code ==== |
Often times, methods exist that are just delegating to some other method, either in the same object or a composed object. | Often times, methods exist that are just delegating to some other method, either in the same object or a composed object. | ||
Line 411: | Line 412: | ||
The => operator has de facto become the "maps to this expression" | The => operator has de facto become the "maps to this expression" | ||
- | The author favors using " | + | The use of the '' |
- | As of this time, this RFC is to use " | + | ===== Related RFCs ===== |
- | + | ||
- | ==== Related RFCs ==== | + | |
A number of other RFCs in active consideration would complement short functions. | A number of other RFCs in active consideration would complement short functions. | ||
- | === Piped functions === | + | ==== Piped functions |
The [[rfc: | The [[rfc: | ||
Line 441: | Line 440: | ||
</ | </ | ||
- | Which is a really nice way to build up a pipeline through composition. | + | Which is a really nice way to build up a pipeline through composition. |
- | === clone-with === | + | <code php> |
+ | function doAThing(User $u) => $u | ||
+ | |> step1(?) | ||
+ | |> step2(?) | ||
+ | |> step3($val, ?) | ||
+ | |> step4(?, $var) | ||
+ | ; | ||
+ | </ | ||
+ | |||
+ | |||
+ | ==== clone-with | ||
Although no formal RFC has been proposed, Máté had discussed a '' | Although no formal RFC has been proposed, Máté had discussed a '' | ||
Line 451: | Line 460: | ||
{ | { | ||
public function __construct(private int $x, private int $y) {} | public function __construct(private int $x, private int $y) {} | ||
- | + | ||
- | | + | |
public function getX(): int => $this-> | public function getX(): int => $this-> | ||
public function getY(): int => $this-> | public function getY(): int => $this-> | ||
Line 459: | Line 467: | ||
public function withY($y): static => clone($this) with {y: $y}; | public function withY($y): static => clone($this) with {y: $y}; | ||
} | } | ||
+ | </ | ||
Thus making many cases of wither methods just as trivial to write as getter methods. | Thus making many cases of wither methods just as trivial to write as getter methods. | ||
- | |||
- | </ | ||
===== Backward Incompatible Changes ===== | ===== Backward Incompatible Changes ===== | ||
Line 483: | Line 490: | ||
[[https:// | [[https:// | ||
- | |||
- | [[https:// | ||
- | |||
- | Pull request for the spec still to come. | ||
===== Implementation ===== | ===== Implementation ===== |
rfc/short-functions.txt · Last modified: 2021/06/15 22:29 by ilutov