But that's my personal opinion.
fn main() {
let condition = true;
let number = if condition { 5 } else { 6 };
println!("The value of number is: {}", number);
}
MarkMLl
I really like the python version:
myvar = 6 if condition else 7
By switching the if and value around, you can basically set a focus, the value you put first is so to say the "important" value. Also it allows really great chaining:
myvar = a if condition1 \
else b if condition2 \
else c if condition3 \
else d
It's (if used with linebreaks) a very clear syntax that is easy to read and understand and also resembles how you would write conditionals in mathematical notation
Compared to for example the condition first syntax:
myvar = if Condition1 then a
else if condition2 then b
else if condition3 then c
else d;
Here you have always to read the conditions (which might be long expressions themselves) before you get to the value.
It's a question on what a reader might focus, the thing you write first is the thing that the programmer will read and understand at first, while for the second part they might even need to scroll. I think that it is more important to see what are the possible values, over what are the possible conditions.
Actually, discussions like these can be inspiring. I might just think of supporting a similar shorthand for the compiler I'm working on. But it should be clear and different from other statements, perhaps something like:
by condition { number = 5, 6 };
The benefit of such construct is that due to a different keyword (by) it could be processed separately and never reach the expression parser. Just a spontaneous idea.
Personally, just from looking at it, I think this solution isn't very great, because if I didn't have the context on what it is supposed to mean, I wouldn't understand it. Languages should be expressive on their own, reading a program should be as easy as possible.
Also I think it would be really cluttered if it was chained