-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Different rules for conditional function call (e.g. fn? x ) #4292
Comments
This is a feature, not a bug, and IMO a very important feature of CoffeeScript. An example usage of the existential operator from the CS docs: zip = lottery.drawWinner?().address?.zipcode Or Whenever I write code in JavaScript instead of CoffeeScript, this is the number one feature I miss. :) |
Thanks for your comment @carlmathisen. I fully agree with you that conditional function call is a very good feature. I'm not saying it should be removed, but rather that it's implementation should be different. Example you have provided works just as well in both current and proposed implementation, unless you expect that Consider how existential family of operators in CoffeeScript is consistent, except for the function invocation:
What I suggest is to change the meaning of |
Ok, I've just realised that original title for this issue wasn't the best I could have come up with :) |
This has come up before (e.g. #2315, and I'm sure there are more recent ones). There's been no consensus so far. Personally I agree that the semantics should be the same for function calls as they are for other existence checks (e.g |
I was very much surprised today to learn that this code doesn't throw:
I was expecting it to throw
TypeError: fn is not a function
. Turns out it compiles to:IMO the language is too lenient here for it's user's own sake. The above example is quite certainly an application developers mistake and an error would be very helpful. Current implementation can lead to a very subtle bugs.
I suggest it compiles to this instead:
Real life example
I had a perfectly good piece of code like this:
Then I decided that it will read better if I put the conditional logic on new lines:
Now my code was compiling and running without errors, but the left branch of the
if
statement was never being executed. You can imagine that in a large codebase it was quite hard to figure out where does the wrong value come from.The text was updated successfully, but these errors were encountered: