www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - opSlice Bug?

reply "Namespace" <rswhite4 googlemail.com> writes:
Why I have to write
arr2[] += arr[][]
instead of
arr2[] += arr[]
? Bug or 'feature'? :P

Code:
http://dpaste.dzfl.pl/4c732f4c
Nov 29 2012
next sibling parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 11/29/2012 08:15 AM, Namespace wrote:
 Why I have to write
 arr2[] += arr[][]
 instead of
 arr2[] += arr[]
 ? Bug or 'feature'? :P

 Code:
 http://dpaste.dzfl.pl/4c732f4c
opSplice, eh? Is that another undocumented feature or one that has been deprecated? Ali
Nov 29 2012
next sibling parent reply "Namespace" <rswhite4 googlemail.com> writes:
On Thursday, 29 November 2012 at 16:46:45 UTC, Ali Çehreli wrote:
 On 11/29/2012 08:15 AM, Namespace wrote:
 Why I have to write
 arr2[] += arr[][]
 instead of
 arr2[] += arr[]
 ? Bug or 'feature'? :P

 Code:
 http://dpaste.dzfl.pl/4c732f4c
opSplice, eh? Is that another undocumented feature or one that has been deprecated? Ali
http://dlang.org/operatoroverloading.html#Slice
Nov 29 2012
parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 11/29/2012 08:50 AM, Namespace wrote:
 On Thursday, 29 November 2012 at 16:46:45 UTC, Ali Çehreli wrote:
 On 11/29/2012 08:15 AM, Namespace wrote:
 Why I have to write
 arr2[] += arr[][]
 instead of
 arr2[] += arr[]
 ? Bug or 'feature'? :P

 Code:
 http://dpaste.dzfl.pl/4c732f4c
opSplice, eh? Is that another undocumented feature or one that has been deprecated? Ali
http://dlang.org/operatoroverloading.html#Slice
Thanks. I really did read it as opS_p_lice, found a few references on dlang.org, none on any spec page and got a little frustrated. I did not realize that I had clearly misread it. I had even written some notes about opSlice: http://ddili.org/ders/d.en/operator_overloading.html Apparently I had thought that it better returned a special type: struct Range { // ... } struct Container { // For the object[] syntax Range opSlice() { Range allElements; // ... must provide access to all elements ... return allElements; } /* ... */ } Ali
Nov 29 2012
parent "Namespace" <rswhite4 googlemail.com> writes:
Yes, that could be better (but I don't like it), but my code 
should compile as well, or not?
Nov 29 2012
prev sibling parent "bearophile" <bearophileHUGS lycos.com> writes:
Ali Çehreli:

 opSplice, eh? Is that another undocumented feature or one that 
 has been deprecated?
I think it's not deprecated: http://dlang.org/operatoroverloading.html#Slice Bye, bearophile
Nov 29 2012
prev sibling parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 11/29/2012 08:15 AM, Namespace wrote:
 Why I have to write
 arr2[] += arr[][]
 instead of
 arr2[] += arr[]
 ? Bug or 'feature'? :P

 Code:
 http://dpaste.dzfl.pl/4c732f4c
Apparently, the array-wise operations require the following syntax: a[] += b[]; In other words, the compiler wants to see [] on the right-hand side as well. The following does not work with the same error message that you get: int[] a, b; a[] += b; Error: invalid array operation a[] += b (did you forget a [] ?) It is the same in your case. You have a function that returns a slice but the compiler still wants to see [] on the rigth-hand side: int[] foo() { int[] b; return b; } void main() { int[] a; a[] += foo(); // <-- Same compilation error } That's why you need to put the [] after it: a[] += foo()[]; I think it is just the requirement of the syntax. (?) Ali
Nov 29 2012
parent "Namespace" <rswhite4 googlemail.com> writes:
That is what I thought also.
That is awfully and limited. :/
Nov 29 2012