-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
gotocase: a case for useful and not harmful goto #5950
Comments
I think the more idiomatic zig way for handling that example would be to define const pmask = switch (xyz) {
.Point => {
// more complex code
}
.Mask => getBitMask(),
};
// code for drawing
|
Why only cases that are further down? Seems like an unnecessary restriction, and along with only working inside switches makes this feature very edgecasey. We could implement this from more flexible components with a |
switch (abc) {
.Car => {
foo("car");
bar(42);
baz(1337);
},
.Train => {
foo("train");
bar(42);
baz(1337);
},
} If they were exactly the same, we could just combine them. switch (abc) {
.Car, .Train => {
foo("transportation thing idk kthxbai");
bar(42);
baz(1337);
},
} But we can solve this problem without needing fn doThing(name: []const u8) void {
foo(name);
bar(42);
baz(1337);
}
switch (abc) {
.Car => doThing("car"),
.Train => doThing("train"),
} Furthermore, a prominent example of the linked website on the first issue comment displays the usage of |
I agree with @joachimschmidt557, introducing even just forward Giving up on this transparency is a pretty big deal and would require a commensurate benefit, which I don't see here. At the very least, a better example is needed, since the given one can be implemented in better ways even in C++, let alone Zig. |
I agree with the previous two posters. I don't think this is a good fit
for Zig.
…-- Email domain proudly hosted at https://migadu.com
|
So, switch (val) {
.case1 => continue .case3,
.case2 => do_case2(),
.case3 => do_case1_and_3(),
} |
@ekipan, as I mentioned in my comment earlier, what you wrote can be achieved without switch (val) {
.case2 => do_case2(),
.case1, .case3 => do_case1_and_3(),
} |
@joachimschmidt557 Sure, I was just talking syntax though. I've no opinion on the feature itself. Multicase prongs can't do this: switch (val) {
.case1 => do_case1(); continue .case3,
.case2 => do_case2(),
.case3 => do_case1_and_3(),
} But again you could just use functions: switch (val) {
.case1 => do_case1(); do_case1_and_3(),
.case2 => do_case2(),
.case3 => do_case1_and_3(),
} |
The use case I thought this was going to address is implementing something like computed
Where it would allow us to express the above with something like
On one hand we lose the cue from "while" that there is effectively a loop. |
@raulgrell, the original proposal here is much more limited in scope than what you have in mind. By restricting jumps to be forward only, it allows avoiding certain kinds of duplication, but prevents most kinds of spaghetti code. If backward jumps were to be allowed, arbitrary control flow could be implemented, like the FSM in your example. At that point, we are dealing with a full-powered |
The use case in this proposal is sufficiently addressed by the comments in this issue as well as #8220. |
While it's obvious that goto is considered harmful, sometimes it is still a usefull tool.
Because zig dosen't support switch falltrough, i propose an alternative: gotocase.
Let's consider this esample from https://wiki.c2.com/?GoTo in C
This obviously saves time and looks more elegant than combination of if statements and functions.
Perhaps with gotocase it could look like this:
To enforce good code practices one could restrict gotocase to apear only at the end of the case and only cases that are further down.
The text was updated successfully, but these errors were encountered: