Thread overview | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
August 21, 2004 proposal: operator for push back on array? | ||||
---|---|---|---|---|
| ||||
Hello. I know we already have ~= for character arrays and int arrays. However they don't work for class/struct arrays and character arrays of arrays. Right now, If I want to add something to a class or array of character arrays i have do to this... // this is what I do items.length = items.length+1; // first I add one to the array items[length-1] = "string"; // then I set the last index to what I want What I'd like to see is an operator that works like push_back() in c++ vectors. Where all you'd have to type is items &&= "string"; // i think &= is already used? or items pushback "string"; or items #= "string"; or something to that effect, i'm not sure about what operator would fit nicely. What do you think? Or should we just leave this up to templates (DTL?) with a items.pushback("string"); syntax? it seems nicer having it built in though. |
August 21, 2004 Re: proposal: operator for push back on array? | ||||
---|---|---|---|---|
| ||||
Posted in reply to clayasaurus | clayasaurus wrote: > Hello. I know we already have ~= for character arrays and int arrays. However they don't work for class/struct arrays and character arrays of arrays. Can you post an example? I just tried char[][] items; items ~= "item1"; items ~= "item2"; printf("%d %.*s %.*s\n",items.length,items[0],items[1]); and got the (correct) answer 2 item1 item2 > Right now, If I want to add something to a class or array of character arrays i have do to this... > > // this is what I do > items.length = items.length+1; // first I add one to the array > items[length-1] = "string"; // then I set the last index to what I want > > What I'd like to see is an operator that works like push_back() in c++ > vectors. > > Where all you'd have to type is > > items &&= "string"; // i think &= is already used? > or > items pushback "string"; > or > items #= "string"; > > or something to that effect, i'm not sure about what operator would fit nicely. > > What do you think? Or should we just leave this up to templates (DTL?) > with a items.pushback("string"); syntax? > > it seems nicer having it built in though. |
August 21, 2004 Re: proposal: operator for push back on array? | ||||
---|---|---|---|---|
| ||||
Posted in reply to clayasaurus | "clayasaurus" <clayasaurus@gmail.com> wrote in message news:cg6h7a$15h3$1@digitaldaemon.com... > Hello. I know we already have ~= for character arrays and int arrays. However they don't work for class/struct arrays and character arrays of arrays. > But ~= works for all arrays. class Foo {} int main() { char[][] strings; strings ~= "mystring"; strings ~= "foo"; strings ~= "etc"; Foo[] fa; fa ~= new Foo; return 0; } Or am I misunderstanding you? |
August 21, 2004 Re: proposal: operator for push back on array? | ||||
---|---|---|---|---|
| ||||
Posted in reply to clayasaurus | "clayasaurus" <clayasaurus@gmail.com> wrote in message news:cg6h7a$15h3$1@digitaldaemon.com... > Hello. I know we already have ~= for character arrays and int arrays. However they don't work for class/struct arrays and character arrays of arrays. > > Right now, If I want to add something to a class or array of character arrays i have do to this... > > // this is what I do > items.length = items.length+1; // first I add one to the array > items[length-1] = "string"; // then I set the last index to what I want > > What I'd like to see is an operator that works like push_back() in c++ > vectors. > > Where all you'd have to type is > > items &&= "string"; // i think &= is already used? > or > items pushback "string"; > or > items #= "string"; > > or something to that effect, i'm not sure about what operator would fit nicely. I'm more than a little surprised that ~= does not work for arrays of all types. Are you sure that's correct? (Not casting aspersions; just finding hard to believe) > What do you think? Or should we just leave this up to templates (DTL?) > with a items.pushback("string"); syntax? > > it seems nicer having it built in though. I think ~= should work for all arrays. |
August 21, 2004 Re: proposal: operator for push back on array? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Vathix | Vathix wrote:
> "clayasaurus" <clayasaurus@gmail.com> wrote in message
> news:cg6h7a$15h3$1@digitaldaemon.com...
>
>>Hello. I know we already have ~= for character arrays and int arrays.
>>However they don't work for class/struct arrays and character arrays of
>>arrays.
>>
>
>
> But ~= works for all arrays.
>
>
> class Foo {}
>
> int main()
> {
> char[][] strings;
> strings ~= "mystring";
> strings ~= "foo";
> strings ~= "etc";
>
> Foo[] fa;
> fa ~= new Foo;
>
> return 0;
> }
>
>
> Or am I misunderstanding you?
>
Maybe I am just dumb or something. But anyway
import std.stdio;
struct Bob
{
char[] bob;
}
int main(char[][] args)
{
Bob[] bob;
Bob bob1, bob2;
bob1.bob = "jim";
bob2.bob = "bob";
bob ~= bob1 ~= bob2; // this does not work
//bob.length = 2; // this does
//bob[0] = bob1;
//bob[1] = bob2;
writefln(bob[0].bob);
writefln(bob[1].bob);
return 0;
}
I get the error "Can only concatenate arrays," but is bob not an array? *confused*
|
August 21, 2004 Re: proposal: operator for push back on array? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ben Hinkle | Ben Hinkle wrote: > clayasaurus wrote: > > >>Hello. I know we already have ~= for character arrays and int arrays. >>However they don't work for class/struct arrays and character arrays of >>arrays. > > > Can you post an example? I just tried > char[][] items; > items ~= "item1"; > items ~= "item2"; > printf("%d %.*s %.*s\n",items.length,items[0],items[1]); > and got the (correct) answer > 2 item1 item2 > > Aha. Well what I'd like is just to do char[] items; items ~= "item1"; items ~= "item2"; printf("%d %.*s %.*s\n",items.length,items[0],items[1]); ang get the correct answer. with this i get seg fault yay. withouth the ~= operator, of course. |
August 21, 2004 Re: proposal: operator for push back on array? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Matthew | Matthew wrote:
> I'm more than a little surprised that ~= does not work for arrays of all types. Are you sure that's correct? (Not
> casting aspersions; just finding hard to believe)
>
What I would like (like push back in c++ vector) is for it to add one to the array length and then fill it with given value.
like
char[] items;
items add "bob";
items add "monkey";
and have items become an array the length of 2 with
items[0] = "bob"
and
items[1] = "monkey"
|
August 21, 2004 Re: proposal: operator for push back on array? | ||||
---|---|---|---|---|
| ||||
Posted in reply to clayasaurus | clayasaurus wrote:
> Hello. I know we already have ~= for character arrays and int arrays.
> However they don't work for class/struct arrays and character arrays of arrays.
>
> Right now, If I want to add something to a class or array of character arrays i have do to this...
>
> // this is what I do
> items.length = items.length+1; // first I add one to the array
> items[length-1] = "string"; // then I set the last index to what I want
>
> What I'd like to see is an operator that works like push_back() in c++ vectors.
>
> Where all you'd have to type is
>
> items &&= "string"; // i think &= is already used?
> or
> items pushback "string";
> or
> items #= "string";
>
> or something to that effect, i'm not sure about what operator would fit nicely.
>
> What do you think? Or should we just leave this up to templates (DTL?) with a items.pushback("string"); syntax?
>
> it seems nicer having it built in though.
The trick is that ~= appends a single element to an array, whereas ~ combines two arrays:
Spam[] ni;
Spam[] eggs;
eggs ~= new Spam(); // ok
eggs = ni ~ ni ~ ni; // ok
eggs ~= ni; // no
eggs = eggs ~ new Spam(); // no
It's inconsistent, but it's better than the alternative:
Spam temp = new Spam();
eggs ~= &(temp)[0..1]
:)
-- andy
|
August 21, 2004 Re: proposal: operator for push back on array? | ||||
---|---|---|---|---|
| ||||
Posted in reply to clayasaurus | clayasaurus wrote: > > Ben Hinkle wrote: > > clayasaurus wrote: > > > > > >>Hello. I know we already have ~= for character arrays and int arrays. However they don't work for class/struct arrays and character arrays of arrays. > > > > > > Can you post an example? I just tried > > char[][] items; > > items ~= "item1"; > > items ~= "item2"; > > printf("%d %.*s %.*s\n",items.length,items[0],items[1]); > > and got the (correct) answer > > 2 item1 item2 > > > > > > Aha. Well what I'd like is just to do > > char[] items; This can hold only one string. > items ~= "item1"; > items ~= "item2"; > printf("%d %.*s %.*s\n",items.length,items[0],items[1]); item[0], item[1] refernce the first and second character. printing in string format uses character as adresses > ang get the correct answer. with this i get seg fault yay. yes, a fault is to be expected. > withouth the ~= operator, of course. -- Helmut Leitner leitner@hls.via.at Graz, Austria www.hls-software.com |
August 21, 2004 Re: proposal: operator for push back on array? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andy Friesen | In article <cg6k22$18s7$1@digitaldaemon.com>, Andy Friesen says... >The trick is that ~= appends a single element to an array, whereas ~ combines two arrays: > > Spam[] ni; > Spam[] eggs; > > eggs ~= new Spam(); // ok > eggs = ni ~ ni ~ ni; // ok > eggs ~= ni; // no > eggs = eggs ~ new Spam(); // no Is that really true? Wow. I had assumed that # a ~= b; was equivalent to: # a = a ~ b; Are you saying it isn't? >It's inconsistent, but it's better than the alternative: > > Spam temp = new Spam(); > eggs ~= &(temp)[0..1] It's inconsistent, and therefore I would never have guessed in a million years that it might work. Hell, for all I know now, maybe += and + behave differently from each other. Do I have to try them all to find out? I've always done: # eggs.length = eggs.length + 1; # eggs[eggs.length-1] = new Spam(); My preference would be that /both/ ~ /and/ ~= should be overloaded, in the obvious way. For all types T: # T[] ~ T[] // concatenate two arrays # T[] ~ T // append a single element # T ~ T // call opCat(), or compile-error with ~= behaving identically. I don't think this leads to any ambiguity, does it? Arcane Jill |
Copyright © 1999-2021 by the D Language Foundation