Properties and std.container.Array
Frustrated
c1514843 at drdrb.com
Thu Jan 9 13:38:13 PST 2014
On Thursday, 9 January 2014 at 14:51:33 UTC, monarch_dodra wrote:
> On Thursday, 9 January 2014 at 13:32:08 UTC, Dicebot wrote:
>> On Thursday, 9 January 2014 at 12:19:25 UTC, Frustrated wrote:
>>> I guess I see what is going on. Since Array is a struct, a
>>> local copy is made and that never ends up updating the
>>> original?
>>>
>>> How can I use it then like an object so this is not a problem?
>>
>> returning by ref may do what you want:
>>
>> @property std.container.Array!int arr() { return _arr; }
>> ->
>> @property ref std.container.Array!int arr() { return _arr; }
>
> As dicebot says, however, the issue is a bit more subtle.
>
> An `Array` is actually little more than a pointer to a payload.
> Passing by value is "almost" free, and updating a copy *should*
> update the original...
>
> ...that is, if it wasn't for the "Gotcha" that the `Array`
> needs to be initialized first.
>
> But overall, by ref should be just fine.
I thought about using ref after the fact. It is a significant
rework of my code but it does seem to work. I am not initializing
the Arrays.
It seems that a better approach maybe to wrap the Array in a
class and use alias this? If Array does need to be
initialized(seems to work fine without it but maybe a memory
leak?) this could be done in the class.
What I worry about is end up with a bunch of copies of the array
and none of them updated properly. It doesn't seem quite right to
have it as a struct.
More information about the Digitalmars-d-learn
mailing list