I noticed that the optional parentheses in Coffeescript introduce a syntactic ambiguity.
For example, the code
prompt Math.max(2), 3 # no space after max
compiles to
prompt(Math.max(2), 3); // one parameter to max(), two to prompt()
whereas
prompt Math.max (2), 3 # space after max
compiles to
prompt(Math.max(2., 3)); // two parameters to max(), one to prompt()
I.e. adding a space before the parameter list changes the grouping of the parameters.
Is this intentional? Are there other places in the language where a space makes a syntactic difference?
Yes, it is intentional.
Space after variable in CofeeScript means: “apply the following list of arguments to this function.
Lets look to your code:
It means: “apply to
prompttwo arguments:Math.max(2)and3“.It means: “apply to
Math.maxtwo arguments:(2)and3, and then apply the result toprompt“.Lets see more complex example:
In this example bracket are necessary.
This feature allows you to write the same code in two ways:
is the same as
I don’t know about any other places in the language, where a space makes a syntactic difference. But there is a place, where brackets makes difference:
and