[Issue 11988] Add __switch symbol to allow retrieval of switch statement value
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Fri Jan 24 13:30:25 PST 2014
https://d.puremagic.com/issues/show_bug.cgi?id=11988
monkeyworks12 at hotmail.com changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |monkeyworks12 at hotmail.com
--- Comment #1 from monkeyworks12 at hotmail.com 2014-01-24 13:30:21 PST ---
(In reply to comment #0)
> Often you would want to use the value of the switch statement, but the only way
> to do this is to store the value to a variable first. This has the unfortunate
> effect of invading the outer scope:
>
> -----
> import std.string;
>
> int get() { return 0; }
>
> void a(int) { }
> void b(int) { }
> void c(int) { }
>
> void main()
> {
> int v = get();
> switch (v)
> {
> case 1: a(v); break;
> case 2: b(v); break;
> case 3: c(v); break;
>
> default:
> assert(0, format("Unhandled case %s", v));
> }
>
> // problem: v is still visible here
> }
> -----
>
> To avoid invading the outer scope, but at the same time avoiding introduction
> of arbitrary language features, it might be useful to introduce another
> compiler-reserved symbol "__switch". The above code would then look like:
>
> -----
> import std.string;
>
> int get() { return 0; }
>
> void a(int) { }
> void b(int) { }
> void c(int) { }
>
> void main()
> {
> switch (get())
> {
> case 1: a(__switch); break;
> case 2: b(__switch); break;
> case 3: c(__switch); break;
>
> default:
> assert(0, format("Unhandled case %s", __switch));
> }
> }
> -----
>
> The default diagnostic is where I've encountered a need for this feature, very
> frequently too.
Why don't we just allow `auto var = <expression>`, like in an if-statement?
--
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
More information about the Digitalmars-d-bugs
mailing list