www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - proposal: operator for push back on array?

reply clayasaurus <clayasaurus gmail.com> writes:
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.
Aug 20 2004
next sibling parent reply Ben Hinkle <bhinkle4 juno.com> writes:
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.

Aug 20 2004
parent reply clayasaurus <clayasaurus gmail.com> writes:
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.
Aug 20 2004
parent Helmut Leitner <helmut.leitner wikiservice.at> writes:
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
Aug 20 2004
prev sibling next sibling parent reply "Vathix" <vathixSpamFix dprogramming.com> writes:
"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?
Aug 20 2004
parent reply clayasaurus <clayasaurus gmail.com> writes:
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*
Aug 20 2004
parent reply "Ivan Senji" <ivan.senji public.srce.hr> writes:
"clayasaurus" <clayasaurus gmail.com> wrote in message
news:cg6i74$16oj$1 digitaldaemon.com...
 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

To do what you want to do write: bob ~= bob1; bob ~= bob2; Although i would think taht this would work too: (bob ~= bob1) ~= bob2; but it doesn't :(
    //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*

Aug 21 2004
parent reply clayasaurus <clayasaurus gmail.com> writes:
I guess my confusion arouse out of thinking

bob ~= bob1 ~= bob2;

would be the same as

bob ~= bob1;
bob ~= bob2;

and I got confused on the difference between

char[] and char[][].

Thanks for all the replies!

Ivan Senji wrote:
 "clayasaurus" <clayasaurus gmail.com> wrote in message
 news:cg6i74$16oj$1 digitaldaemon.com...
 
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

To do what you want to do write: bob ~= bob1; bob ~= bob2; Although i would think taht this would work too: (bob ~= bob1) ~= bob2; but it doesn't :(
   //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*


Aug 21 2004
parent reply "Matthew" <admin.hat stlsoft.dot.org> writes:
"clayasaurus" <clayasaurus gmail.com> wrote in message
news:cg7h9v$1vfu$1 digitaldaemon.com...
 I guess my confusion arouse out of thinking

 bob ~= bob1 ~= bob2;

 would be the same as

 bob ~= bob1;
 bob ~= bob2;

Quite understandable. I fail to see why it should not. If ~= does not return its lhs as an lvalue, then what on earth _does_ it return? Most confusing.
 and I got confused on the difference between

 char[] and char[][].

 Thanks for all the replies!

 Ivan Senji wrote:
 "clayasaurus" <clayasaurus gmail.com> wrote in message
 news:cg6i74$16oj$1 digitaldaemon.com...

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

To do what you want to do write: bob ~= bob1; bob ~= bob2; Although i would think taht this would work too: (bob ~= bob1) ~= bob2; but it doesn't :(
   //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*



Aug 21 2004
parent Charles Hixson <charleshixsn earthlink.net> writes:
Matthew wrote:
 "clayasaurus" <clayasaurus gmail.com> wrote in message
news:cg7h9v$1vfu$1 digitaldaemon.com...
 
I guess my confusion arouse out of thinking

bob ~= bob1 ~= bob2;

would be the same as

bob ~= bob1;
bob ~= bob2;

Quite understandable. I fail to see why it should not. If ~= does not return its lhs as an lvalue, then what on earth _does_ it return? Most confusing.

void, I think. (I must admit to not having tried this, but it's one of two standard conventions. The other would have it returning a string, which would allow: if ((bob ~= bob) == "bob) then... But I seem to remember an explicit decision against that. If you want that try: bob ~= bob1 ~ bob2; But this wouldn't change the value of bob1. And bob had better already have a legal value (would null work here? Probably).
Aug 23 2004
prev sibling next sibling parent reply "Matthew" <admin.hat stlsoft.dot.org> writes:
"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.
Aug 20 2004
parent clayasaurus <clayasaurus gmail.com> writes:
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"
Aug 20 2004
prev sibling parent reply Andy Friesen <andy ikagames.com> writes:
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
Aug 20 2004
parent reply Arcane Jill <Arcane_member pathlink.com> writes:
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
Aug 20 2004
parent reply Andy Friesen <andy ikagames.com> writes:
Arcane Jill wrote:

 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?

In any setting where an array can contain an element of its own type, yes. (dynamic types, arrays inheriting Object, et cetera) That is never the case in D now, so it should be okay. It basically boils down to whether or not D wants to keep those sorts of possibilities open. Personally, I think ~ and ~= should concatenate arrays *only*. Array literal syntax covers the rest: Spam[] spam, eggs; spam ~= eggs; // ok now spam ~= [new Spam()]; // concat anon array of 1 spam = [new Spam(), new Spam()]; // concatenate two elements into one array -- andy
Aug 21 2004
parent Ilya Minkov <minkov cs.tum.edu> writes:
Andy Friesen schrieb:

 Personally, I think ~ and ~= should concatenate arrays *only*.
 
 Array literal syntax covers the rest:
 
     Spam[] spam, eggs;
     spam ~= eggs;         // ok now
     spam ~= [new Spam()]; // concat anon array of 1
     spam = [new Spam(), new Spam()]; // concatenate two elements into 
 one array

Agree and vote! -eye
Aug 23 2004