Associative array .get with .init as default second argument

Torarin torarind at gmail.com
Mon Oct 18 12:47:50 PDT 2010


The AA .get function is defined like this:

    Value get(Key key, lazy Value defaultValue)
    {
        auto p = key in *cast(Value[Key]*)(&p);
        return p ? *p : defaultValue;
    }

Can we also have an overload that uses Value's init value as fallback?
That would simplify checks like this:

    if (headers.get("transfer-encoding", "") == "chunked")
    if (headers.get("transfer-encoding") == "chunked")

This is what STL map's operator [] does, and I think it's handy. It
only requires the addition of this overload:

    Value get(Key key)
    {
        auto p = key in *cast(Value[Key]*)(&p);
        return p ? *p : Value.init;
    }

It would also make these kind of accesses more efficient, since you
skip the lazy delegate.


More information about the Digitalmars-d mailing list