-
Notifications
You must be signed in to change notification settings - Fork 205
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
Allow control statements to be an expression #1211
Comments
See also #352, #132 and some of #307 and #27. The challenge with making control statements be expressions is that expressions must have a value, but statement don't inherently have one. The solution here seems to be using the value of the expression of last expression statement of the statement. var x = test1
? expression1
: test2
? expression2
: test3
? expression3
: expression4; A more interesting example would be something using a loop statement or a switch. var x = switch (kind) {
case Enum.number: 4; break;
case Enum.string: "string"; break;
case Enum.bool: true; break;
} or var x = while (y > 0) { x += y--; x; } We have had other requests for "expression-switch", which would make sense. The rules for an expression switch would be that the case-blocks would be expressions, not statements, and the switch must be exhaustive (or we default to For a do-while loop, we could ensure that the body has a value, but a Changing |
Thanks for the explanation 👍 , since this is already requested I will close it. |
Currently the result of control statements like
if
ortry/catch
can not be assigned into a variable.I know that there are some operators that helps with simple cases like
?
but when each of the cases is bigger then the line code tends to be bigger than one or two lines and it can become a mess.button
variable in the example asfinal
in the declaration, helping again expressing the intention of the code.Current 1
Lets say we have a
build
function that builds a widget with some parameters in one case and with others in other case:Current 2
It can be used in simple cases but the indentation becomes tricky and does not support more than 2 different cases.
Desired
As you can see it assigns the result into the variable declaration.
Some references
The text was updated successfully, but these errors were encountered: