Short lambdas / arrow functions offer a convenient, compact way to write simple closures as a single expression. This RFC offers the same convenience for named functions and methods that are simple return expressions.

This RFC provides an alternate, abbreviated syntax for functions and methods, designed to mimic the syntax of short lambdas. Specifically, the first function below is semantically identical to the second:

function add(int $a, int $b): int => $a + $b;
 
function add(int $a, int $b): int 
{
    return $a + b;
}

The same abbreviated form is also available for methods. Both of the following methods are semantically identical.

class Adder
{
    public function __construct(private int $val) {}
 
    public function add(int $in): int => $in + $this->val;
 
    public function add(int $in): int {
        return $in + $this->val;
    }
}

Functions are simpler than lambdas, as there is no need for closing over variables contextually. Therefore this patch is implemented 100% in the lexer, and thus should have no performance impact whatsoever.

Php shortcodes should be available in 8.1 if approved. More information at https://wiki.php.net/rfc/short-functions

LEAVE A REPLY

Please enter your comment!
Please enter your name here