"return ... when ..." syntax #10425
Replies: 3 comments
-
To me having a line that, depending on a condition, either exits the scope or continues execution extremely confusing and error prone, your example is pretty simple but something like: for x in y:
return foo when bar()
baz()
return buz() Would be very confusing and hard to read At least to me having a single line, instead of two, hurts readability, and shouldn't be discouraged, but if you absolutely want that why not just: for x in y:
if bar(): return foo
baz()
return buz() Also do you know of any other language that has this construction? I can't remember seeing it elsewhere, having other languages that use this would be more of an indication that it's a good idea (or rather, the lack of other examples would be an indication it isn't) |
Beta Was this translation helpful? Give feedback.
-
Not a fan of having to read the line in the opposite order, it's unusual, and particularly so for GDScript which doesn't have anything simil- |
Beta Was this translation helpful? Give feedback.
-
I have found only proposals, not implementations in other languages. It would be similar to nested ternary operators, which is already possible in GDScript and is standard practice in Haskell. The weakness of ternary operators is that it's only ideal for static operations; the else expression is always evaluated. For it being backwards, I've found variable assignment backwards for getting storage location before having the value ready. I do understand this is going against decades of programming practices. Anyway, it looks like this is very unpopular. I don't mind closing this. |
Beta Was this translation helpful? Give feedback.
-
This is probably a more controversial idea, so I'm writing this here.
"when" is already a keyword used for match. I think it would be nice to apply it to return statements. I can understand the flow of functions faster when there's less indents and important keywords such as "return" are furthest to the left.
It would allow statements such as
... to be implemented as
Beta Was this translation helpful? Give feedback.
All reactions