Thread overview | |||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
October 03, 2008 Static Constructors | ||||
---|---|---|---|---|
| ||||
I've been using classes for a few months now and I keep on making arrays of instances like: class Fruit { this( parameters ) { } .. } Fruits fruit[]; void init() { fruit.length=fruit.length+1; // annoys me :) fruit[$]=new Fruit( .. ); } deleting is even more annoying. Now I've been reading about static constructors and is that somehow a solution to this manual array management? |
October 03, 2008 Re: Static Constructors | ||||
---|---|---|---|---|
| ||||
Posted in reply to Saaa | Reply to Saaa, > I've been using classes for a few months now and I keep on making > arrays of instances like: > > class Fruit > { > this( parameters ) > { > } > .. > } > Fruits fruit[]; > > void init() > { > fruit.length=fruit.length+1; // annoys me :) > fruit[$]=new Fruit( .. ); > } > deleting is even more annoying. > > Now I've been reading about static constructors and is that somehow a > solution to this manual array management? > Sort of: switch "void init" for "static this" and you won't need to call init from main, but that's about all you will get. Also you can not control the order of execution. 2 minor nits: > fruit[$]=new Fruit( .. ); should be > fruit[$-1]=new Fruit( .. ); typo? If you are going to be adding more than very few items to the array, pick a better allocation scheme uint at = 0; if(extra <= at) data.length = (at+1)*2; data[at] = t; at++; //when done adding data.length = at; another option would be to use an AA if order isn't relevant. T[int] data; void reg(T b) { static int a = 0; data[a++] = b; } |
October 03, 2008 Re: Static Constructors | ||||
---|---|---|---|---|
| ||||
Posted in reply to Saaa | On Fri, Oct 3, 2008 at 5:57 PM, Saaa <empty@needmail.com> wrote: > I've been using classes for a few months now and I keep on making arrays of instances like: > > class Fruit > { > this( parameters ) > { > } > .. > } > > Fruits fruit[]; > > void init() > { > fruit.length=fruit.length+1; // annoys me :) > fruit[$]=new Fruit( .. ); > } Have you heard of the append operator? fruit ~= new Fruit(...); > > deleting is even more annoying. You don't have to delete anything. This is what the GC is for. > > Now I've been reading about static constructors and is that somehow a solution to this manual array management? I don't know what you're trying to do, so it may or may not be a solution. |
October 03, 2008 Re: Static Constructors | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jarrett Billingsley | Reply to Jarrett,
> On Fri, Oct 3, 2008 at 5:57 PM, Saaa <empty@needmail.com> wrote:
>
>> deleting is even more annoying.
>>
> You don't have to delete anything. This is what the GC is for.
>
delete could be "remove element 42 from an array of 54 resulting an an array of 53"
data = data[0..42] ~ data[43..$];
or
data[42..$-1] = data[43..$].dup; // in-place sortof
data.length = data.length-1;
|
October 04, 2008 Re: Static Constructors | ||||
---|---|---|---|---|
| ||||
Posted in reply to BCS | >> I've been using classes for a few months now and I keep on making arrays of instances like: >> >> class Fruit >> { >> this( parameters ) >> { >> } >> .. >> } >> Fruits fruit[]; >> >> void init() >> { >> fruit.length=fruit.length+1; // annoys me :) >> fruit[$]=new Fruit( .. ); >> } >> deleting is even more annoying. >> >> Now I've been reading about static constructors and is that somehow a solution to this manual array management? >> > > Sort of: switch "void init" for "static this" and you won't need to call init from main, but that's about all you will get. Also you can not control the order of execution. > > 2 minor nits: > >> fruit[$]=new Fruit( .. ); > should be >> fruit[$-1]=new Fruit( .. ); > > typo? yeah :), originally I did fruit[].length-1. > > If you are going to be adding more than very few items to the array, pick a better allocation scheme > > uint at = 0; > > if(extra <= at) data.length = (at+1)*2; extra=data.length; ? > data[at] = t; > at++; > > > //when done adding > data.length = at; Initially I'd be adding a lot, but after that only sparsely. > > another option would be to use an AA if order isn't relevant. > > T[int] data; Is this code? I mean should there be an int or 'int'? > > void reg(T b) > { > static int a = 0; > data[a++] = b; How does this work when it's no dynamic array? > } |
October 04, 2008 Re: Static Constructors | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jarrett Billingsley | > > Have you heard of the append operator? > > fruit ~= new Fruit(...); Yeah, but never used it other than with strings . . silly me. > >> >> deleting is even more annoying. > > You don't have to delete anything. This is what the GC is for. I think I don't get GC use. How do I actually delete one of those fruits then? |
October 04, 2008 Re: Static Constructors | ||||
---|---|---|---|---|
| ||||
Posted in reply to BCS |
>>
>>> deleting is even more annoying.
>>>
>> You don't have to delete anything. This is what the GC is for.
>>
>
> delete could be "remove element 42 from an array of 54 resulting an an array of 53"
>
> data = data[0..42] ~ data[43..$];
> or
> data[42..$-1] = data[43..$].dup; // in-place sortof
> data.length = data.length-1;
>
Yes, like that :)
What is the difference between those two? I mean, what do you mean by
in-place.
|
October 04, 2008 Re: Static Constructors | ||||
---|---|---|---|---|
| ||||
Posted in reply to Saaa | On Sat, 04 Oct 2008 16:34:56 +0400, Saaa <empty@needmail.com> wrote:
>
>>>
>>>> deleting is even more annoying.
>>>>
>>> You don't have to delete anything. This is what the GC is for.
>>>
>>
>> delete could be "remove element 42 from an array of 54 resulting an an
>> array of 53"
>>
>> data = data[0..42] ~ data[43..$];
>> or
>> data[42..$-1] = data[43..$].dup; // in-place sortof
>> data.length = data.length-1;
>>
>
> Yes, like that :)
> What is the difference between those two? I mean, what do you mean by
> in-place.
>
>
First one allocates new memory block.
Second one attempts to erase an element in-place. Dupping, however, allocates new memory, too.
The best solution would be as follows:
void eraseNth(ref T[] data, int n) {
data[n] = data[$-1];
data.length = data.length - 1;
}
|
October 04, 2008 Re: Static Constructors | ||||
---|---|---|---|---|
| ||||
Posted in reply to Saaa | Saaa:
> I think I don't get GC use. How do I actually delete one of those fruits then?
The GC deletes an object (usually, but sometimes it doesn't do it because it's not a precise GC) when there are no references to/from it.
So if you keep it just inside an array, and it has no inbound references, and you remove it somehow (for example overwriting it with the last item of the array and then reducing the length of the array by 1), the GC loses the only reference to that objects, and deallocates it.
Note the same thing happens in most languages/systems that have a GC.
If your object manages some other resources beside its memory, for example an open file, you have to close it inside the destructor of your object. If the not precise GC keeps a spurious (wrong) reference to your object (because somewhere an int value looks like a pointer to the object memory block), then you are in troubles...
Bye,
bearophile
|
October 04, 2008 Re: Static Constructors | ||||
---|---|---|---|---|
| ||||
Posted in reply to Denis Koroskin |
>
> First one allocates new memory block.
> Second one attempts to erase an element in-place. Dupping, however,
> allocates new memory, too.
> The best solution would be as follows:
>
> void eraseNth(ref T[] data, int n) {
> data[n] = data[$-1];
> data.length = data.length - 1;
> }
Yay, thats how I do it :)
|
Copyright © 1999-2021 by the D Language Foundation