spider icon indicating copy to clipboard operation
spider copied to clipboard

Conditional Expression without else

Open alongubkin opened this issue 10 years ago • 6 comments

var v = x if a;

compiles to:

var v = x if a else null;

which compiles to:

var v = a ? x : null;

alongubkin avatar Nov 29 '14 10:11 alongubkin

will this work? (x if a) == 5

Namek avatar Nov 29 '14 12:11 Namek

@Namek yes

alongubkin avatar Nov 29 '14 12:11 alongubkin

+1. Will it still work if if <expr> then <expr> else <expr> #98 is to be added?

mationai avatar Nov 29 '14 17:11 mationai

I have rarely ever though I would want something like this, ternary expressions never felt wrong. But when I have more than one condition the ternary mode can become a mess. This is not the best example in the world but it illustrates my point.

var size = useLarge ? "large" : useSmall ? "small" : "medium";

rewritten with the proposed form

var size = "large" if useLarge else "small" if useSmall else "medium";

formats nicely too

var size = "large" if useLarge else 
           "small" if useSmall else 
           "medium";

andrewluetgers avatar Nov 30 '14 17:11 andrewluetgers

@andrewluetgers :heart:

Namek avatar Nov 30 '14 19:11 Namek

+1

nmn avatar Dec 01 '14 19:12 nmn