Have you ever written something like this:
function a(int $one, int $two) {
return $one + $two;
}
function addFive(int $one) {
return a($one, 5);
}
// or
$addFive = fn(int $one) => a($one, 5);
With partial function application this will reduce to $addFive = a(?, 5);.
Nested functions will be a bit more readable.
$slug = fn(str $str) => strtolower(implode('-',str_word_count($str,1)));
// versus
$slug = strtolower(implode('-',word_count(?,1)));
And a great benefit will be seen in pipe operator code.
$slug = $str
|> (fn($str) => str_word_count($str,1))
|> (fn($str) => implode('-',$str))
|> (fn($str) => strtolower($str));
// versus
$slug = $str |> str_word_count(?,1) |> implode('-', ?) |> strtolower(?);
An extra benefit of the partial function application implementation is that it adds the static keyword to the the closure it creates.
For the people that don't know, adding the static keyword improves performance.
Beside the question mark, an elipsis is also an partial function application placeholder. This is most beneficial for functions or methods with at least three arguments.
function a(int $one, int $two, int $three) {
return $one + $two + $three;
}
$minimumFive = a(5, ...);
// alternative
$mimimumFive = a(5, ?, ?);
$minimumFive(6,7);
On the one hand having the question marks makes it clear what the signature of the underlying function is. On the other hand developers are lazy typists.
If you want more information read the RFC. This will be in PHP 8.6.
I think additions like this make PHP a fun language to work with.
Top comments (0)