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

Joerg Joergonson via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Jun 20 14:35:18 PDT 2016


On Monday, 20 June 2016 at 16:27:29 UTC, Steven Schveighoffer 
wrote:
> 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

Thanks, this is what I was thinking I'd have to do. I'm probably 
going to manually manage the array items. I just need simple 
append and remove but this will still help with the drop in 
replacement(for my cases).









More information about the Digitalmars-d-learn mailing list