Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No-Brace Short Closure not working #7

Open
HallofFamer opened this issue Sep 18, 2018 · 2 comments
Open

No-Brace Short Closure not working #7

HallofFamer opened this issue Sep 18, 2018 · 2 comments

Comments

@HallofFamer
Copy link

HallofFamer commented Sep 18, 2018

It seems that short closure without braces aint working for me. This code cannot be compiled into the correct syntax in PHP:

$result = array_map($x ~> $x * 2 * ++$y, range(1, 10));

Instead it has parse error: 'syntax error, unexpected '~', expecting ',' or ')''. Apparently the issue is with expression() identifier which doesnt seem to work, its a bug with Yay library. I wonder if you have a workaround for this issue, or we may have to wait a response from marcioAlmada to fix Yay. Or if you dont have the same problem as I do, lemme know too.

@HallofFamer
Copy link
Author

HallofFamer commented Sep 21, 2018

By performing the following tests I was able to find out what the issue is:

$filtered = array_filter($amplified, $fruit ~> in_array($fruit, $exclude)); // it works
$filtered = array_filter($amplified, $fruit ~> !in_array($fruit, $exclude)); // it does not work

$result = array_map($x ~> pow($x, 2), range(1, 10)); // it works
$result = array_map($x ~> $x * 2 * ++$y, range(1, 10)); // it does not work

Seems that the yay parser will fail if you apply operators(arithmetic, logic, or whatever) in a simple expression. I dont think this can be resolved without a bug fix from Yay, so at this moment a lesser workaround is to allow wrapping complex expressions with parenthesis(). Here is what I did:

buffer("~>"),
expression() as body,
_() as scope

Changes the middle line like this:

buffer("~>"),
either(expression(), chain(buffer("("), layer() ,buffer(")"))) as body,
_() as scope

With this change, the below expression will work properly:

$filtered = array_filter($amplified, $fruit ~> (!in_array($fruit, $exclude))); // it works now

With all being said, the workaround is not pretty, I hope this issue will be resolved completely following an update from Yay.

@assertchris
Copy link
Member

Sorry I didn't see this sooner. It's a curious bug...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants
@assertchris @HallofFamer and others