Object arrays in D

CrudOMatic crudomatic at gmail.com
Tue Apr 10 03:01:10 PDT 2012


On Tuesday, 10 April 2012 at 09:52:45 UTC, simendsjo wrote:
> On Tue, 10 Apr 2012 11:27:19 +0200, CrudOMatic 
> <crudomatic at gmail.com> wrote:
>> hmmmm... so no need for a generic object reference counter. 
>> Nice. I was trying to approach it from a literal sense (e.g. 
>> card moves from deck to player's hand) - when you do the 
>> popBack() is that object returned and deleted from the 
>> original array, or just the reference returned and ref removed 
>> from original array?
>
> The reference is returned, and it's still in the array - kind 
> of... Say:
> cards is [Card1, Card2]
> cards.popBack();
> cards is now [Card1], but Card2's reference is still there in 
> position 2. The length of the array in decremented.
> If you try to append a card to cards, the runtime will see 
> you're trying to write over the reference for Card2, so it will 
> create a new array to avoid this.
> If you use assumeSafeAppend, you say that you don't mind 
> overwriting this reference.
> Remember that as long as you hold a reference to Card2 
> somewhere, the GC won't delete the instance even if the 
> reference no longer exists in the cards array.
>
>> I was trying to use literal real-world ideas of objects, and 
>> was wanting the moving so I didn't have weird issues later 
>> down the road, like not knowing why a reference was hanging 
>> around. I understand D has a GC, but sometimes it pays to do 
>> management yourself.
>
> A card game doesn't sound like heavy use of the GC, so I don't 
> think that should be a problem.

Awesome. One last question, does popFront() have the same effect 
of decreasing length - or should I avoid popFront()?


More information about the Digitalmars-d mailing list