RE: [PHP-DEV] [RFC] [Discussion] Short Closures

From: Date: Tue, 01 Sep 2015 10:37:24 +0000
Subject: RE: [PHP-DEV] [RFC] [Discussion] Short Closures
References: 1  Groups: php.internals 
Request: Send a blank email to internals+get-87991@lists.php.net to get a copy of this message
Hi Bob, > I had this RFC in draft since some time, but delayed it due to all the ongoing PHP 7 > discussions. Also we have no master branch to merge features in until 5.4 EOL. Thus I'm > reviving this now. > > Time for the first RFC targeting PHP 7.1 (assuming PHP 8 isn't going to be the next > version ;-)): > > The short Closures RFC: > https://wiki.php.net/rfc/short_closures > > Hoping for constructive feedback, > Bob Whilst I'd like to see a more expressive syntax for closures (and many other things) in PHP, I'm personally -1 on your proposal. The feature feels too inconsistent with the syntax and semantics of current PHP constructs for the following reasons: 1) The automatic binding of variables from the outer scope to the inner scope. 2) The optional use of parentheses for parameter definitions, but only when a single argument is used. 3) The optional use of braces for the function body, but only when a single expression is used. 4) The automatic returning of the expression, but only when it is alone, and only if the braces are omitted. These four differences create further special cases for people to learn and have no precedence in the language at all. They seem more like Rubyisms than PHPisms. Whilst we're on the topic of a terser syntax for closures though, Elixir has an even shorter syntax: &(&1 + &2) The &() denotes an closure definition; the &N is used to create placeholders, where N corresponds to the argument number. The ampersand obviously cannot be reused for this in PHP, but the dollar sign could: $func = $($1 + $2); var_dump($func(3, 4)); // int(7) This syntax should only be used in trivial scenarios, though, and comes with the disadvantage of no type information. Thanks, Tom

« previous php.internals (#87991) next »