November 13, 2001 two questions | ||||
---|---|---|---|---|
| ||||
These appeared when trying to actually write a simple program in D, so somewhat more practical... Is there a simple and (probably) fast way to add a value at the end of a dynamic array? Documentation doesn't say anything about that specifically, although there are mentions of append() method when describing strings. Does it work on any arrays? Is there a method that removes last element of dynamic array? Can "const" attribute be applied to function's return type? An example: class Component { public const Component[] Children() { return children; } private Component[] children; } As you can see, I want children[] to be readable, but not writeable from outside of class (so that noone can add child components but the component itself). Can it be implemented this (or any other) way? And an idea. I suggest that using enumerations when declaring arrays would declare arrays with number of elements equal to number of enumeration members: enum color { red, green, blue }; bit[color] c; // equal to bit[color.max].c; Sometimes this can be useful, especially with bit arrays. It also serves as at least partial emulation of Pascal sets. |
November 13, 2001 Re: two questions | ||||
---|---|---|---|---|
| ||||
Posted in reply to Pavel \"EvilOne\" Minayev |
Pavel \"EvilOne\" Minayev wrote:
>
> These appeared when trying to actually write a simple program in D, so somewhat more practical...
>
> Is there a simple and (probably) fast way to add a
> value at the end of a dynamic array? Documentation
> doesn't say anything about that specifically, although
> there are mentions of append() method when describing
> strings. Does it work on any arrays? Is there a method
> that removes last element of dynamic array?
I believe that Perl uses negative array indices to mean
"back from the end", so array[-1] refers to the last
valid element of the array. With bounds-controlled arrays,
I would think that negative indices would be free for
this use, though it would occasionally turn an erroneous
index calculation into a valid-yet-incorrect access (or,
looked at another way, it turns a bounded array into a
partial circular array). I can see a lot of arguments
against including this construct in D. :)
-RB
|
November 14, 2001 Re: two questions | ||||
---|---|---|---|---|
| ||||
Posted in reply to Pavel \"EvilOne\" Minayev | "Pavel "EvilOne" Minayev" <evilone@omen.ru> wrote in message news:9srme1$331$1@digitaldaemon.com... > Is there a simple and (probably) fast way to add a > value at the end of a dynamic array? I'm looking at: int a[]; a[] ~= 3; >Is there a method > that removes last element of dynamic array? a = a[0..a.length - 1]; > Can "const" attribute be applied to function's return > type? An example: No. Const is a storage class in D, not a type modifier. > class Component > { > public const Component[] Children() { return children; } > private Component[] children; > } > > As you can see, I want children[] to be readable, but not > writeable from outside of class (so that noone can add > child components but the component itself). Can it be > implemented this (or any other) way? The way to do that is to make it private and access it through get/set member functions. > And an idea. I suggest that using enumerations when declaring > arrays would declare arrays with number of elements equal > to number of enumeration members: > > enum color { red, green, blue }; > bit[color] c; // equal to bit[color.max].c; > > Sometimes this can be useful, especially with bit arrays. It also serves as at least partial emulation of Pascal sets. That's a great idea, but it conflicts with the syntax of associative arrays. |
November 14, 2001 Re: two questions | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | "Walter" <walter@digitalmars.com> wrote in message news:9sth7s$194b$1@digitaldaemon.com... > I'm looking at: > int a[]; > a[] ~= 3; But wouldn't it interfere with the rule that: "In general, (a[n..m] op e) is defined as: for (i = n; i < m; i++) a[i] op e;" Could lead to confusion... > >Is there a method > > that removes last element of dynamic array? > > a = a[0..a.length - 1]; I believe it's not a special, optimized case recognized by the compiler. I mean something to remove the element without actually relocating the entire array - in other words, something that simply decreases its "virtual" size without actually changing anything. > > As you can see, I want children[] to be readable, but not > > writeable from outside of class (so that noone can add > > child components but the component itself). Can it be > > implemented this (or any other) way? > > The way to do that is to make it private and access it through get/set member functions. But that's exactly what I did! I just want it to be indexed, but not changed. And yes, I know that I could just write some methods for that... but wouldn't it be better to provide common syntax for all such cases? And since I can't overload operator[], there is no way to do it other than expose the array itself. Maybe some special attribute similar to C/C++ const specially for arrays & pointers? > > And an idea. I suggest that using enumerations when declaring > > arrays would declare arrays with number of elements equal > > to number of enumeration members: > > That's a great idea, but it conflicts with the syntax of associative arrays. Oh, sorry, I missed the point. Maybe for bit arrays only (I mean, associative bit arrays in D are not different from int ones... or are they?) |
November 14, 2001 Re: two questions | ||||
---|---|---|---|---|
| ||||
Posted in reply to Pavel \"EvilOne\" Minayev | > Maybe some special attribute similar to C/C++ const specially for arrays & pointers?
One more thing.
It seems quite strange to me that, while D provides
ways to declare variable passed to function as read-only
in that function, it doesn't have analogous construct
for what function returns...
|
November 14, 2001 Re: two questions | ||||
---|---|---|---|---|
| ||||
Posted in reply to Pavel \"EvilOne\" Minayev | Pavel \"EvilOne\" Minayev wrote: > "Walter" <walter@digitalmars.com> wrote in message news:9sth7s$194b$1@digitaldaemon.com... > > > I'm looking at: > > int a[]; > > a[] ~= 3; > > But wouldn't it interfere with the rule that: > > "In general, (a[n..m] op e) is defined as: > > for (i = n; i < m; i++) > a[i] op e;" > > Could lead to confusion... > > > >Is there a method > > > that removes last element of dynamic array? > > > > a = a[0..a.length - 1]; > > I believe it's not a special, optimized case recognized > by the compiler. I mean something to remove the element > without actually relocating the entire array - in other > words, something that simply decreases its "virtual" size > without actually changing anything. Why not a.length--; -- The Villagers are Online! villagersonline.com .[ (the fox.(quick,brown)) jumped.over(the dog.lazy) ] .[ (a version.of(English).(precise.more)) is(possible) ] ?[ you want.to(help(develop(it))) ] |
November 14, 2001 Re: two questions | ||||
---|---|---|---|---|
| ||||
Posted in reply to Pavel \"EvilOne\" Minayev | Pavel \"EvilOne\" Minayev wrote:
>
> "Walter" <walter@digitalmars.com> wrote in message news:9sth7s$194b$1@digitaldaemon.com...
> > >Is there a method
> > > that removes last element of dynamic array?
> >
> > a = a[0..a.length - 1];
>
> I believe it's not a special, optimized case recognized
> by the compiler. I mean something to remove the element
> without actually relocating the entire array - in other
> words, something that simply decreases its "virtual" size
> without actually changing anything.
It's presumably as easy, or easier, for Walter to make the compiler recognize the a = a[0..X] case and optimize it as it would be for him to add syntax for a special case of same. If the initial index of the slice is computed, furthermore, the slice generator could notice a computed zero at runtime that couldn't be guaranteed at compile time.[1]
Finally, it saves the programmer having to learn, remember, and appropriately-use a new feature.
-RB
[1] In fact, if you want to get really ambitious, the internal "start of allocated buffer" and "array-zero-base" pointers could actually be separate, and things like:
a = a[2..a.length];
could simply alter the length and zero-base pointers without doing a realloc (since saving two bytes hardly seems worth the time and potential fragmentation).
|
November 14, 2001 Re: two questions | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | > The way to do that is to make it private and access it through get/set member functions. I don't see how get/set functions would help this, okay you do not need to return 'const obj **' (C speaking) an array of constant objects... but you would still need to return the object beeing requested by get_obj(object_number), like in your case 'obj *' (C speaking again), but how can I tell the caller without a const attribute in the sense of "contract programming" "please just look, but don't touch that object"? - Axel -- |D) http://www.dtone.org |
November 15, 2001 Re: two questions | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | "Walter" <walter@digitalmars.com> wrote in message news:9sth7s$194b$1@digitaldaemon.com... > > "Pavel "EvilOne" Minayev" <evilone@omen.ru> wrote in message news:9srme1$331$1@digitaldaemon.com... > > Is there a simple and (probably) fast way to add a > > value at the end of a dynamic array? > > I'm looking at: > int a[]; > a[] ~= 3; You sure you wouldn't it rather be more like this?: a ~= 3; It seems a, being the identifier representing the entire array, known to the compiler to be dynamic, wouldn't need the []? Why ~=, out of curiosity? += isn't ambiguous in that context. I don't see what the problem is with people not looking up the declarations of variables they're unfamiliar with. > >Is there a method > > that removes last element of dynamic array? > > a = a[0..a.length - 1]; What's wrong with: --a.size; Oh... probably not writable. or maybe: a.resize(a.size-1); No, too wordy. Perhaps: a--; The "copying" syntax you suggest seems to leave it up to the compiler to realize it can avoid the copy. Debug builds could get very slow like that. Sean |
November 15, 2001 Re: two questions | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sean L. Palmer | > What's wrong with: > > --a.size; > > Oh... probably not writable. > or maybe: > > a.resize(a.size-1); > > No, too wordy. Perhaps: > > a--; There is always a philsophic discussion how "decorated" a standard API should be, minimal (small is beautiful) or enlarged with a function for every possible task (even is reduntant. I personally think the most intuative way would be a.shrink(1); - Axel -- |D) http://www.dtone.org |
Copyright © 1999-2021 by the D Language Foundation