-
Notifications
You must be signed in to change notification settings - Fork 155
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
x ? (y > 0 ? 1 : 2) : 3; // problem in LiveScript #933
Comments
To restate the problem: This code fails to compile (with an
The bug is The 'correct' syntax is to put the entire inner block inside the cascade.
Alternatively, you can group the inner ternary operator with parens to
and I'm not sure, but I have a feeling that the problem comes from the ambiguity of the |
Oh, I see. Used your second version of "workaround" with (). I think JS ternary operator style is good (simple), but LiveScript's version more complicated. |
I feel that having a multi-line if-then block is actually more straightforward from the reader's perspective. JS ternary seems too clever and terse. Of course, LS is the king of terse among the languages targeting JavaScript, but that's not necessarily a good thing either. :) When I say 'clever' I mean code that looks like "Look ma, no hands!" where the same thing can be expressed using a more common syntax (e.g., if-then block). |
I've coded in languages that only has the ternary operator, no if-then, so who's to say what's common syntax ;-) Anyway, I've never seen I would spontaneously have written it like so: x = y > 0 && (z > 0 && 1 || 2) || 3 But as we all know, this is walking on thin ice in JS - but I still find it cleanest. And ofc. in LS: watch out for Alternatively, like so: x = if y > 0 => (if z > 0 => 1 else 2) else 3 And if it must be multi-line, what I think should be the "right" way doesn't work in LS (I find this to be a huge flaw in the language actually!) # Eaaak - LS can't handle this, boohoo
x = if y > 0
if z > 0
1
else
2
else
3 I see no reason for conflict in allowing the above in the language, so I think it's just a flaw/oversight in the compiler implementation. Anyone enlighten me? So, in practise, if having to write it multiline, it would perhaps be: x = if y > 0
if z > 0
1
else
2
else
3 But that's not pretty. |
nice comment ozra, your example: looks complicated) and will work with numbers better to my taste. the case, when you need some more cases) in comparison may look more complicated.. suppose, its all about thens aligned with elses..
i believe, would be hard work to implement in LS. only the one who know the lexer could possible do that) |
The |
That is the only thing I miss from JS syntax, the conciseness of the ternary operator. If someone's interested, this is how I usually format those operations:
but if I can, and if it fits better, I use the implicit switch (the example shows the equivalent code to the previous one):
I hope someone will find it helpful. |
x = if y > 0
__then if z > 0
____then 1
____else 2
__else 3
Hello, this, i think might be a problem.. Should work.
The text was updated successfully, but these errors were encountered: