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

Optional @?x fails to parse #1327

Closed
STRd6 opened this issue Jul 23, 2024 · 5 comments · Fixed by #1352
Closed

Optional @?x fails to parse #1327

STRd6 opened this issue Jul 23, 2024 · 5 comments · Fixed by #1352
Labels
bug Something isn't working good first issue Good for newcomers

Comments

@STRd6
Copy link
Contributor

STRd6 commented Jul 23, 2024

@?trigger "changed"

Currently fails with a parse error but should parse as

this?.trigger("changed")

In strict mode this can be null or undefined.

@STRd6 STRd6 added bug Something isn't working good first issue Good for newcomers labels Jul 23, 2024
@edemaine
Copy link
Collaborator

Oddly, CoffeeScript treats @?trigger "changed" like @? trigger "changed", i.e., as two function calls. This is pretty weird though.

This is related to a question I asked a while back on Discord: should x?y be shorthand for x?.y, just like how x?(y) is shorthand for x?.(y) and x?[y] is shorthand for x?.[y]? I think making @?x shorthand for this?.x is less controversial, though, because @x is already shorthand for this.x which includes the dot.

@vendethiel
Copy link
Contributor

vendethiel commented Jul 24, 2024

Oddly, CoffeeScript treats @?trigger "changed" like @? trigger "changed", i.e., as two function calls. This is pretty weird though.

It's self-consistent - the parens are invisible, not the dot.
@?foo is this && this(foo) in CoffeeScript, not this && this.foo() (just like a?b is a && a(b), not a && a.b). Coffee considers @ to be callable (which, well, is fair enough.)

@STRd6
Copy link
Contributor Author

STRd6 commented Jul 24, 2024

Hmm, we may want to go for consistency with CoffeeScript then, or at least note it and have a compat flag.

@vendethiel
Copy link
Contributor

vendethiel commented Jul 24, 2024

I'd say CoffeeScript is not consistent with itself here: the only call introducers are supposed to be whitespace and (.

Coco and LiveScript did change this (a?b is a?.b), though I'm not 100% sure it was meant to change the meaning of a?b then (Coco 0.4.2: "Enabled ADI on ?.", but the commit adds a test without removing or changing another one)

@edemaine
Copy link
Collaborator

TIL CoffeeScript treats f?g like f? g also. So I agree it's somewhat consistent on this point, but it doesn't match my own intuitive definition of implicit calls which would have matched @vendethiel's:

the only call introducers are supposed to be whitespace and (.

However, CoffeeScript's actual definition is vague:

You don’t need to use parentheses to invoke a function if you’re passing arguments. The implicit call wraps forward to the end of the line or block expression.

I don't see any CoffeeScript tests about this, nor could I find an issue.

But when we define a meaning for @?foo and/or foo?bar, I agree we should probably add a coffeeCompat flag, even though I couldn't find any real-world usage.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants