Allow any expression / statement in "if expressions" #11660
Replies: 2 comments
-
I think this will complicate the language because when reading code of this style your brain must simultaneously deals with branches and locals/returns inside them. And about Python's ternary style. I code with Python, but I don't like it very much, because when reading ternary people natually want to first see the condition and next the branches. Consider this var := aaaa+bbbb-cccc*dddd/(eeee*ffff)-gggg if abc && def && ghi else hhhh+iiii-jjjj versus this var := if abc && def && ghi { aaaa+bbbb-cccc*dddd/(eeee*ffff)-gggg } else { hhhh+iiii-jjjj } I have to say I perfer the latter, even though it's slightly longer. If just for brevity, see C's style. var := abc && def && ghi ? aaaa+bbbb-cccc*dddd/(eeee*ffff)-gggg : hhhh+iiii-jjjj I'm OK with this style. The downside is, exceeded brevity makes it sometimes difficult to find the separation of conditions and branches. |
Beta Was this translation helpful? Give feedback.
-
@Anthony-Gaudino this will be supported. |
Beta Was this translation helpful? Give feedback.
-
V version: 0.1.25 ab3f6d9
Currently using an
if expression
and trying, for example, to declare a variable inside it throws an error:This results in:
I believe programmers would expect they could do anything they can in an if statement also in an
if expression
and the last expression in anif expression
would be the value yield. So this for example should be allowed:OR
Maybe a keyword like
return
could be used insideif expressions
to make the return value more obvious:So
if expressions
would work more similar to a lambda / anonymous function.To do the same in Javascript would require the more verbose:
Rust supports it:
And last but not least, I believe that having a shorter
if expression
option with the same syntax as Python (Python ternary operator) would be very nice.Python ternary operator has the form:
Which is very friendly, slight more concise, allows chaining expressions and won't need adding another keyword like
then
to the language.With it, this would be valid in V:
This too would be valid:
Currently V transpiles
if expressions
to C ternary expressions, in these cases this should still be true:The Python
if expression
syntax, if implemented, wouldn't allow declaring variables as it wouldn't fit well with it's syntax.In the case of declaring a variable inside an
if expression
:it's not supported in C so it must be transpiled to something else, maybe a function.
Beta Was this translation helpful? Give feedback.
All reactions