[Issue 11070] Allow auto statement in a switch statement
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Thu Sep 19 13:16:09 PDT 2013
http://d.puremagic.com/issues/show_bug.cgi?id=11070
bearophile_hugs at eml.cc changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |bearophile_hugs at eml.cc
--- Comment #1 from bearophile_hugs at eml.cc 2013-09-19 13:16:07 PDT ---
(In reply to comment #0)
> Additionally, such a variable
> shouldn't be visible outside the switch statement if it's only going to be used
> inside of the switch statement.
That's easy solved:
auto get() { return "c"; }
void main() {
import std.string;
{
auto res = get;
switch (res) {
case "a": break;
case "b": break;
default: assert(0, format("Unhandled case: '%s'", res));
}
}
// res not visible here.
}
> This will also eliminate the need for code duplication in switch statements
> which use a common label statement, for example:
>
> -----
> import std.string;
> import std.stdio;
>
> auto get() { return "d"; }
>
> void main()
> {
> switch (auto res = get())
> {
> case "a": goto common;
> case "b": goto common;
> case "c": goto common;
>
> common:
> writefln("Got result: %s", res);
> break;
>
> default: assert(0, format("Unhandled case: '%s'", res));
> }
> }
Usually common bodies for case statements are not a big problem in D:
auto get() { return "d"; }
void main() {
import std.stdio, std.string;
{
auto res = get;
switch (res) {
case "a", "b", "c":
writefln("Got result: %s", res);
break;
default:
assert(0, format("Unhandled case: '%s'", res));
}
}
// res not visible here.
}
--
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
More information about the Digitalmars-d-bugs
mailing list