gkz / LiveScript

LiveScript is a language which compiles to JavaScript. It has a straightforward mapping to JavaScript and allows you to write expressive code devoid of repetitive boilerplate. While LiveScript adds many features to assist in functional style programming, it also has many improvements for object oriented and imperative programming.
http://livescript.net
MIT License
2.32k stars 155 forks source link

Incorrect compilation of or statement #1067

Closed danielo515 closed 6 years ago

danielo515 commented 6 years ago

Hello,

Today I found a weird behavior on livescript that involves the or operator and function execution. Here is a piece of code to demonstrate it:

(actions[\something] || defaultAction) (msg)

This should compile to

(actions['something'] || defaultAction)(msg);

But instead it compiles to

actions['k'](msg) || defaultAction(msg);

Is this a bug ? Can you suggest a workaround ?

Thanks and regards

vendethiel commented 6 years ago

Yeaaah so... That was made for the match feature, which is to this day still undocumented.

danielo515 commented 6 years ago

Oh, so it is intentional... I think it is a bit weird and very away from the usual JS semantics. It may be a about personal preference but I don't see an scenario where the actual behavior is better than the expected behavior. Anyway, the only possible workaround that I see is to assign the result to a variable and then call that variable, which is a pity because I tend to avoid all unnecessary variables.

Regards

rhendric commented 6 years ago

LS has a few tricks for avoiding variables (at least in the source code), if you find any of these appealing:

(if actions[\something] then that else defaultAction) msg
msg |> if actions[\something] then that else defaultAction

or

with actions[\something] or defaultAction then .. msg
danielo515 commented 6 years ago

This last one works like a charm. Thanks @rhendric