I'm usually fairly ambivalent about the idea of statements being expressions, but I would *love* for switch to be usable as an expression. For instance, in Haxe, you can do stuff like the following, which I get a
ton of use out of and often wish D had:

a = switch(b)
{
    case 1: "foo";
    case 2: "bar";
    case 3: "baz";
    case 4: "whee";
    default: "blork";
}

The D equivalents aren't terrible, but they aren't nearly as nice.

This won't work anyway. We are talking about language grammar here. If made expression, statement would be of type void. Just like assert is.

I see what you're saying, but this switch expression should really be of type string.

I certainly wish more things were expressions. "a = if (x) y; else z;" isn't especially useful since we have "a = x ? y : z", but consider instead something that doesn't map so easily to an expression:

// very loosely based on some Android code I wrote recently
dpWidth = _lastKnownWidth =
    if (window.isVisible()) {
        auto m = context.getResources().getSystemMetrics();
        // final statement as value of "if" expr
        window.getWidth() / m.pixelDensity();
    } else if (_lastKnownWidth != 0)
        _lastKnownWidth;
    else
        screenInfo().getWidth();

Or how about:

auto area = {
    auto tmp = foo.bar(baz);
    tmp.width * tmp.height;
}

I also wish "void" were a first-class type with sizeof==0 for maximum efficiency:

    int[void] intSet = [2:(), 3:(), 4:()]

Ditto for size of empty structs. D code should never need abominations like the C++ EBCO.

Reply via email to