-
-
Notifications
You must be signed in to change notification settings - Fork 613
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
Add support for shortened switch #20658
base: master
Are you sure you want to change the base?
Conversation
Thanks for your pull request and interest in making D better, @bangbangsheshotmedown! We are looking forward to reviewing it, and you should be hearing from a maintainer soon.
Please see CONTRIBUTING.md for more information. If you have addressed all reviews or aren't sure how to proceed, don't hesitate to ping us with a simple comment. Bugzilla referencesYour PR doesn't reference any Bugzilla issue. If your PR contains non-trivial changes, please reference a Bugzilla issue or create a manual changelog. Testing this PR locallyIf you don't have a local development environment setup, you can use Digger to test this PR: dub run digger -- build "master + dmd#20658" |
This change will require a DIP. Please post the idea to https://forum.dlang.org/group/dip.ideas before spending too much time here. |
You didn't need a DIP for #20653, why should I? |
Again, this would need to be discussed on the forum or at the very least an issue filed for discussion. I think it's best to keep pull request comments focused on implementation issues. Note that pattern matching is planned, so IMO tweaking switch/case is unlikely to be accepted. |
That will need a DIP at some point. |
You need to write a DIP so the foundation can unofficially reject it (by ignoring it) while they're busy copying C and C++ features. That's important work and mustn't be interrupted by annoying formalities. |
I believe |
I don't know how common it is to have one statement for each case. It is common however to have a one-line return, but that's already clean and smooth: enum K = 42;
switch (K)
{
case 42: return 0;
default: return 1;
} |
The proposal needs some clarification:
should rewrite to:
to make the issue of variable scopes clear. |
@WalterBright I have no idea how to do that, I'm sorry I'll need someone to help me |
Can you please share a code example? I'm not sure what to test exactly |
switch (K)
{
case 42 => int x;
default => x++; // should error, `x` not found
} |
I guess the code already handles it, because it doesn't compile:
|
You're right, the scopes don't overlap. Perhaps @WalterBright has another example? |
Duff's Device:
The |
It's unfortunate that it's too late to forbid such painful-to-read pessimizations like duff's device in D... |
this one too works void send_1(short* to, short* from, int count)
{
auto n = (count + 7) / 8;
switch (count % 8) {
case 0: do { *to = *from++;
goto case;
case 7: *to = *from++;
goto case;
case 6: *to = *from++;
goto case;
case 5: *to = *from++;
goto case;
case 4: *to = *from++;
goto case;
case 3: *to = *from++;
goto case;
case 2: *to = *from++;
goto case;
case 1: *to = *from++;
continue;
} while (--n > 0);
break;
default: break;
}
}
void send_2(short* to, short* from, int count)
{
auto n = (count + 7) / 8;
switch (count % 8) {
case 0 => do { *to = *from++;
goto case;
case 7 => *to = *from++;
goto case;
case 6 => *to = *from++;
goto case;
case 5 => *to = *from++;
goto case;
case 4 => *to = *from++;
goto case;
case 3 => *to = *from++;
goto case;
case 2 => *to = *from++;
goto case;
case 1 => *to = *from++;
continue;
} while (--n > 0);
break;
default: break;
}
} Granted the test case is valid: {
short[4] data = [1,2,3,4];
short[4] output = 0;
send_1(output.ptr, data.ptr, cast(int) data.length);
assert(output[0] == 1 && output[1] == 2 && output[2] == 3 && output[3] == 4);
}
{
short[4] data = [1,2,3,4];
short[4] output = 0;
send_2(output.ptr, data.ptr, cast(int) data.length);
assert(output[0] == 1 && output[1] == 2 && output[2] == 3 && output[3] == 4);
} |
There was a mistake in your code, should be: |
I checked with enum K = 42;
switch (K)
{
case 42 => printf("yes!\n");
default => printf("no!\n");
}
becomes: enum int K = 42;
switch (42)
{
case 42:
{
printf("yes!\n");
break;
}
default:
{
printf("no!\n");
break;
}
} |
|
auto a = switch(b){
case x => 1;
case y => 3;
default => 7;
} Pattern matching: auto a = switch(sum){
case Vec2(x, y) => x+y;
case string c => c.to!int();
default => { assert(0); };
} Essentially the same as this: https://forum.dlang.org/thread/[email protected] |
why did you add shortened functions syntax only just to avoid writing if you want to shit on features, please do it consistently
there is no such thing in D today, please stick to what exist today, so we can improve things today i'm not interested in arguing for 10 years, eg: tuples DIP |
i am working on a PR for switch as expression, let's get this in first |
to people who object, i want to see your PRs for pattern matching, talk is cheap |
They would never be merged because we have no DIP and not everyone is a compiler dev. I’m waiting for Rikki’s match statement DIP (which I linked above) to have a final decision made. It has been some months so there must’ve been some delays. If Rikki’s DIP is accepted, then we can just use that instead of adding a very similar feature with slightly different syntax. |
Actually today |
clean and smooth