[Issue 11988] New: Add __switch symbol to allow retrieval of switch statement value

d-bugmail at puremagic.com d-bugmail at puremagic.com
Fri Jan 24 12:53:49 PST 2014


https://d.puremagic.com/issues/show_bug.cgi?id=11988

           Summary: Add __switch symbol to allow retrieval of switch
                    statement value
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: DMD
        AssignedTo: nobody at puremagic.com
        ReportedBy: andrej.mitrovich at gmail.com


--- Comment #0 from Andrej Mitrovic <andrej.mitrovich at gmail.com> 2014-01-24 12:53:42 PST ---
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.

-- 
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