about "with statement"

Andrej Mitrovic andrej.mitrovich at gmail.com
Sun Jun 9 04:56:27 PDT 2013


On 6/9/13, bearophile <bearophileHUGS at lycos.com> wrote:
> It's a quite useful statement, especially with enumerations or
> associative array literals, to avoid repeating many times their
> name:
>
> switch (en) with (MyEnum) {
>    case foo: ...
>    case bar: ...
> }

Yep, I use this idiom a lot. It's also very useful in unittests, to
avoid copy-pasting code and polluting the namespace. A contrived
example:

-----
class C
{
    int x, y;
    this() { x = 1; y = 2; }
}

unittest
{
    with (new C)
    {
        assert(x == 1);
        assert(y == 2);
    }
}
-----

There are some bugs with it currently, but I think they're fixable.


More information about the Digitalmars-d mailing list