-
Notifications
You must be signed in to change notification settings - Fork 77
Switch expressions #640
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
base: dev
Are you sure you want to change the base?
Switch expressions #640
Conversation
FINALLY got around to doing some merging. I'm 90% happy with this, but still not sure about the GetDiscount(numitems)
{
assert(numitems > 0);
return switch (numitems;
1, 2, 3: 0; // 1-3 items - full price
4..10: 10; // 4-10 items - 10% off
15 // >10 items - 15% off
);
} That to me more nicely mirrors the |
Forgot to mention, the last |
I'm good with both optional. |
Actually, there is an issue with that: new Tag:a;
new b = switch(thing;
5: 6;
_:a
); Now becomes ambiguous - is that an explicit |
It's an explicit default. If you want an implicit one with a tag override, you can enclose the expression into parentheses. return switch (value;
1: 2;
(_:TaggedResult())
); |
…t from the previously inserted value, don't start over from the beginning of the list
I'm not sure if the optional #include <a_samp>
Five()
{
printf("5");
}
Six()
{
printf("6");
}
main()
{
new a = 5;
new b = switch (a;
5: Five();
6: Six();
7
);
printf("%d", b);
} Also, when I change it to an explicit default: #include <a_samp>
Five()
{
printf("5");
}
Six()
{
printf("6");
}
main()
{
new a = 5;
new b = switch (a;
5: Five();
6: Six();
_: 7;
);
printf("%d", b);
} I get no warnings, which is not good, because there should be warnings that |
I tried without a #include <a_samp>
Five()
{
printf("5");
return 2;
}
Six()
{
printf("6");
return 1;
}
main()
{
new a = 7;
new b = switch (a;
5: Five();
6: Six();
);
printf("%d", b);
} Firstly, that code took a weirdly long time to compile, it should be almost instant, but I was waiting a good few seconds. Then there's no error for missing defaults. If this is by design the fallbacks need to be documented, but I don't think they should be optional in an expression. This code actually printed |
…on duplicate case values
Thanks. I'm already aware of this problem and fixed it locally yesterday, just didn't finish the tests, which is why I haven't uploaded the fix yet.
No, this is not by design. That's strange; even though I wrote most of the code a year and a half ago, I clearly remember making a check for a default branch. Maybe I implemented it in a separate experimental branch and didn't merge it into the main feature branch or something... EDIT: Indeed, it was in a separate branch; not sure why I didn't merge it in - probably because it adds a new error code for situations when a switch expression doesn't contain a default case. |
This issue has been automatically marked as stale because it has not had recent activity. |
What this PR does / why we need it:
This PR implements switch expressions, as proposed in #496.
Examples of use:
Which issue(s) this PR fixes:
Fixes #496
What kind of pull this is:
Additional Documentation: