Re: [RFC]Discuss] Syntax for Arrow Functions

From: Date: Sat, 10 Jun 2017 12:43:49 +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 17 18  Groups: php.internals 
Request: Send a blank email to internals+get-99476@lists.php.net to get a copy of this message
2017-06-10 11:09 GMT+02:00 Stephen Reay <php-lists@koalephant.com>: > > > On 10 Jun 2017, at 15:19, Björn Larsson <bjorn.x.larsson@telia.com> > wrote: > > > > Den 2017-06-09 kl. 22:00, skrev Niklas Keller: > >> 2017-06-09 15:44 GMT+02:00 Sara Golemon <pollita@php.net>: > >> > >>> On Fri, Jun 9, 2017 at 7:23 AM, Björn Larsson < > bjorn.x.larsson@telia.com> > >>> wrote: > >>>> If I take the liberty in using the example above on our option list: > >>>> 1. $someDict->map(fn($v) => $v * 2)->filter(fn($v) => $v % 3); > >>>> 2. $someDict->map(function($v) => $v * 2)->filter(function($v) => > >>>> $v % > >>> 3); > >>>> 3. $someDict->map($v ==> $v * 2)->filter($v ==> $v % 3); > >>>> 4. $someDict->map(($v) => $v * 2)->filter(($v) => $v % 3); > >>>> // > >>>> Ambiguous > >>>> 5. $someDict->map([]($v) => $v * 2)->filter([]($v) => $v % 3); > >>>> > >>>> Old proposals: > >>>> 6. $someDict->map($v ~> $v * 2)->filter($v ~> $v % 3); > >>>> 7. $someDict->map(lambda($v) => $v * 2)->filter(lambda($v) => $v % > >>>> 3); > >>>> > >>> Something else which really pops in these examples is the effect of > >>> not needing to use parentheses when embedding a single-arg short > >>> lambda. 3 and 6 in your list read cleaner to me (due to the lack of > >>> parenthesis clutter). Sadly ~> has the same hacky implementation > >>> issues as ==>, but I think that shows a little bit of why the HackLang > >>> team decided the messy lexer was worth the clearer syntax. > >> > >> Another possible syntax (dunno whether this has already been suggested > >> on-list): > >> > >> $function = { $x => 2 * $x }; > >> $function = { ($x) => 2 * $x }; > >> $function = | $x => 2 * $x |; > >> $function = | ($x) => 2 * $x |; > >> > >> Nikita and Levi prefer it with parenthesis, I prefer it without, > because I > >> think it's unnecessary clutter. > >> > >> A reason to use | ... | instead of { ... } would be to allow future > object > >> literals. > >> > > Applying that to Sara's example becomes: > > 8. $someDict->map({$v => $v * 2})->filter({$v => $v % 3}); > > 9. $someDict->map(|$v => $v * 2|)->filter(|$v => $v % 3|); > > > > I also find that parenthesis makes it cluttered. > > $someDict->map({($v) => $v * 2})->filter({($v) => $v % 3}); > > > > Cheers //Björn > > > > > > -- > > PHP Internals - PHP Runtime Development Mailing List > > To unsubscribe, visit: http://www.php.net/unsub.php > > I find it quite odd that people are claiming paren’s around 0 arguments is > ‘clutter’, but curly braces or pipes around the entire thing is somehow not > clutter. While I understand there are small differences like implicit > return and automatic capturing, having such wildly different syntax to a > regular still doesn’t make much sense to me. The difference is that { ... } or | ... | is required to be unambiguous, otherwise it conflicts with other parser rules. Regards, Niklas

« previous php.internals (#99476) next »