www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - opIndex vs. opSlice for empty slices

reply "Mike Parker" <aldacron gmail.com> writes:
I've always used opSlice to produce empty slices, but having 
recently read the documentation at [1], I see this:

"To overload a[], simply define opIndex with no parameters:"

And no mention that opSlice can fill the same role. Am I right to 
infer that we should prefer opIndex over opSlice for this? If so, 
what's the rationale?


[1] http://dlang.org/operatoroverloading.html#slice
May 21 2015
parent reply "weaselcat" <weaselcat gmail.com> writes:
On Friday, 22 May 2015 at 05:47:28 UTC, Mike Parker wrote:
 I've always used opSlice to produce empty slices, but having 
 recently read the documentation at [1], I see this:

 "To overload a[], simply define opIndex with no parameters:"

 And no mention that opSlice can fill the same role. Am I right 
 to infer that we should prefer opIndex over opSlice for this? 
 If so, what's the rationale?


 [1] http://dlang.org/operatoroverloading.html#slice
http://forum.dlang.org/thread/luadir$t0g$1 digitalmars.com#post-luadir:24t0g:241:40digitalmars.com
May 21 2015
next sibling parent reply "Mike Parker" <aldacron gmail.com> writes:
On Friday, 22 May 2015 at 05:49:17 UTC, weaselcat wrote:
 http://forum.dlang.org/thread/luadir$t0g$1 digitalmars.com#post-luadir:24t0g:241:40digitalmars.com
Thanks!
May 21 2015
parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 05/21/2015 11:20 PM, Mike Parker wrote:
 On Friday, 22 May 2015 at 05:49:17 UTC, weaselcat wrote:
 http://forum.dlang.org/thread/luadir$t0g$1 digitalmars.com#post-luadir:24t0g:241:40digitalmars.com
Thanks!
For convenience, here are the two sections that cover my understanding of this topic: http://ddili.org/ders/d.en/operator_overloading.html#ix_operator_overloading.opSlice http://ddili.org/ders/d.en/templates_more.html#ix_templates_more.overloading,%20operator Ali
May 21 2015
parent "Mike Parker" <aldacron gmail.com> writes:
On Friday, 22 May 2015 at 06:28:16 UTC, Ali Çehreli wrote:
 http://ddili.org/ders/d.en/operator_overloading.html#ix_operator_overloading.opSlice


 http://ddili.org/ders/d.en/templates_more.html#ix_templates_more.overloading,%20operator

 Ali
Thanks, Ali. I've actually looked over that already and it's part of the reason I postes the question in the first place. The docs indicate that opIndex can be used for the empty slice on a single-dimensional array, but your text says it's used for multi-dimensional slicing and opSlice is for the empty, single dimensional form. Then, in the thread linked above, H.S. Teoh's post seems to side with the docs. Additionally, there's the bit in the docs that I missed earlier about the non-templated opSlice being kept for backward compatibility, but the DoubleEndedQueue in your first link use the non-templated opSlice rather than the templated form that H.S. Teoh's post demonstrated. I've only recently started digging into the different sources of documentation on D to understand the why of things and, in the process, have managed to confuse myself about topics I thought I understood. I'm finding it hard to discern which source is correct and why.
May 22 2015
prev sibling parent Steven Schveighoffer <schveiguy yahoo.com> writes:
On 5/22/15 1:49 AM, weaselcat wrote:
 On Friday, 22 May 2015 at 05:47:28 UTC, Mike Parker wrote:
 I've always used opSlice to produce empty slices, but having recently
 read the documentation at [1], I see this:

 "To overload a[], simply define opIndex with no parameters:"

 And no mention that opSlice can fill the same role. Am I right to
 infer that we should prefer opIndex over opSlice for this? If so,
 what's the rationale?


 [1] http://dlang.org/operatoroverloading.html#slice
http://forum.dlang.org/thread/luadir$t0g$1 digitalmars.com#post-luadir:24t0g:241:40digitalmars.com
This is news to me, and very cool! -Steve
May 22 2015