Re: [RFC]Discuss] Syntax for Arrow Functions

From: Date: Thu, 08 Jun 2017 21:20:20 +0000
Subject: Re: [RFC]Discuss] Syntax for Arrow Functions
References: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16  Groups: php.internals 
Request: Send a blank email to internals+get-99464@lists.php.net to get a copy of this message
Den 2017-06-08 kl. 19:21, skrev Fleshgrinder:
On 6/8/2017 6:28 PM, Rasmus Schultz wrote:
it could be a single symbol instead of two
even if this can be done without parser ambiguity, it suffers from visual ambiguity with the assignment operator. consider what this would look like if the expression itself uses the assignment operator...
     f($x) = $y = $y + $x;
versus something like:
     f($x) => $y = $y + $x;
even that is visually ambiguous when used in arrays though:
     $a = [
         "foo" => f($x) => $y = $y + $x,
     ];
also unreadable. even if the parser can cope, I think we need some other symbol that isn't visually ambiguous when used in these contexts?
Absolutely, yes. One thing that could work is a combination of colon and equal sign with an optional type in between:
     f($x, &$y):= $y = $y + $x
     f($x, &$y): int = $y = $y + $x
Still, kind of ambiguous if used with a type. Earlier I used the greater than sign only:
     f($x, $y)> $x > $y
     f($x, $y): bool > $x > $y
As we can clearly see, also not optimal. A thing that should work is a hyphen greater than:
     f(object $o)->$o->property
Looks weird without proper spacing, but with proper spacing ...
     f(object $o) -> $o->property
... it looks fine, imho. Of course, we still have the other options that were mentioned earlier already:
     f ~> 42  // bad on some keyboard layouts
     f ==> 42 // I personally do not like this option
Another possibility is to use a keyword. This would definitely avoid symbol soup and help visual recognition:
     f do 42
     f($x) do $x^3
     f($x, &$y) do $y = $y + $x
However, I am very unsure about this option to be honest. I'd say my choice here is ~> but I'm on a US layout, second is ->. Note that the latter is used in Java, Elixier, Erlang, Haskell, Julia, OCaml, F#, and probably others. Well, one reason ~> came up in the earlier RFC was that ==> was not seen as
quite the best. I liked the tilde option then and do it now. For me that sits on a nordic keyboard layout, I need to use Alt-gr both for $ and ~, extra space for tilde though. So using tilde is no big hassle ;-) So not all liked the syntax then, but there was other things making that RFC fail. If type hinting had been included and variable binding had been slightly different, I think it might have passed. r//Björn

« previous php.internals (#99464) next »