Unmanaged drop in replacemet for [] and length -= 1

Steven Schveighoffer via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Jun 20 09:27:29 PDT 2016


On 6/18/16 5:55 PM, Joerg Joergonson wrote:
> I wanted to switch to std.container.Array but it doesn't seem to mimic
> [] for some odd ball reason. I threw this class together and it seems to
> work.
>
> The only problem is that I can't do
>
> carray.length -= 1;
>
> I can't override `-=` because that is on the class. can I override it
> for length somehow or do I have to create a length wrapper class that
> has it overridden in it? Or is there a way to do it in cArray?

length wrapper *struct*:

struct AdjustableLength
{
    cArray t;
    auto get() { return t.data.length; }
    opOpAssign(string s: "+", Addend)(Addend x)
    {
       //... your code here that does += using t
       t.data.length = get() + x;
    }
    alias get this;
}

@property auto length()
{
    return AdjustableLength(this);
}

D does not have any direct support for modification of properties. It 
has been talked about, but has never been implemented.

-Steve


More information about the Digitalmars-d-learn mailing list