enum

Andrej Mitrovic andrej.mitrovich at gmail.com
Thu Apr 10 07:15:41 PDT 2014


On 4/10/14, Chad Joan <chadjoan at gmail.com> wrote:
> I'd much rather write:
> File myFile;
> myFile.open("foobar.txt", READ | WRITE );
>
> It should be possible, because the argument's context is of the
> (hypothetical) FileAccess enum type.  It should be unambiguous
> that I intend to use FileAccess's "READ" symbol there, and not
> some other symbol from module scope.

There is a possible library workaround for this, e.g. a minimal example:

-----
struct FileAccess
{
    private alias This = typeof(this);

    This opBinary(string op)(This rhs)
    {
        mixin("return This(this.val " ~ op ~ " rhs.val);");
    }

private:
    int val;
}

enum : FileAccess
{
    READ  = FileAccess(1 << 0),
    WRITE = FileAccess(1 << 1)
}

void open(FileAccess access) { }

void main()
{
    open(READ | WRITE);
}
-----


More information about the Digitalmars-d mailing list