On 26/09/2015 17:17, Levi Morrison wrote:
What concerns do you have about `fn($x) => $x * 2` or `function($x) =>
$x * 2`? I will be writing a proper RFC later but I wanted to get
discussion going now.

If a keyword is required next to the parameters, having the => as a separate token looks a bit weird. It no longer matches other languages, so how about thinking a bit further outside the box?

One of the random thoughts that popped into my head during the previous discussion was to base the syntax on the C for-loop, which would also give a place for bound variables without the "use" keyword. e.g. your example could become fn($x;; $x * 2)

I picked "lambda" as the keyword before, and gave these examples:

# lambda(params; bound vars; expression)
$double = lambda($a;; 2*$a)
$x=3; $triple = lambda($a; $x; $x * $a)

function sumEventScores($events, $scores) {
    $types = array_map(lambda($event;; $event['type']), $events);
return array_reduce($types, lambda($sum, $type; $scores; $sum + $scores[$type]));
}


Adding in the type information, we'd get this:

lambda(int $sum, string $type; $scores; $sum + $scores[$type])
# or with fn() if you prefer:
fn(int $sum, string $type; $scores; $sum + $scores[$type])


If return typehints are also required, I'm not sure where they'd best be placed. If they're outside the parens, they end up after the expression, which might look odd:
fn(int $sum, string $type; $scores; $sum + $scores[$type]): int

A few other possibilities:
fn(int $sum, string $type; $scores; $sum + $scores[$type]; int)
fn(int $sum, string $type: int; $scores; $sum + $scores[$type])
fn:int(int $sum, string $type; $scores; $sum + $scores[$type])


All of this assumes that the shorthand is only available for simple expressions, not function bodies, but it seems a bit rendundant to allow both of these:
function($x) { foo(); bar(); baz(); }
fn($x) => { foo(); bar(); baz(); }

And only marginally more useful if variables are auto-captured, with all the downsides of that which have already been raised:
function($x) use ($y) { foo($x); bar($x, $y); }
fn($x) => { foo($x); bar($x, $y); }

Regards,

--
Rowan Collins
[IMSoP]


--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to