August 21, 2004 Re: proposal: operator for push back on array? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Arcane Jill | 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
|
August 21, 2004 Re: proposal: operator for push back on array? | ||||
---|---|---|---|---|
| ||||
Posted in reply to clayasaurus | "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* |
August 21, 2004 Re: proposal: operator for push back on array? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ivan Senji | 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*
>
>
>
|
August 21, 2004 Re: proposal: operator for push back on array? | ||||
---|---|---|---|---|
| ||||
Posted in reply to clayasaurus | "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* > > > > > > |
August 23, 2004 Re: proposal: operator for push back on array? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andy Friesen | 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
|
August 23, 2004 Re: proposal: operator for push back on array? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Matthew | 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).
|
Copyright © 1999-2021 by the D Language Foundation