www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - foreach seems to work with opIndex()

reply Tofu Ninja <emmons0 purdue.edu> writes:
Foreach seems to work if there is an opIndex() with no arguments 
that returns a range interface, is this documented? I can't seem 
to find anything that say this is supposed to happen. I am not 
really complaining, its nice, but I just didnt really expect it 
because I feel like I remember this being an error some time ago.
Feb 06 2016
next sibling parent Tofu Ninja <emmons0 purdue.edu> writes:
On Saturday, 6 February 2016 at 14:43:52 UTC, Tofu Ninja wrote:
 Foreach seems to work if there is an opIndex() with no 
 arguments that returns a range interface, is this documented? I 
 can't seem to find anything that say this is supposed to 
 happen. I am not really complaining, its nice, but I just didnt 
 really expect it because I feel like I remember this being an 
 error some time ago.
To clerify, I was doing something like struct someStruct{ ... dchar[] opIndex(){ if(data==null) return null; else return data.some_array; } } ... someStruct s; foreach(c;s) {} I expected to need to write foreach(c;s[]) {}
Feb 06 2016
prev sibling next sibling parent reply anonymous <anonymous example.com> writes:
On 06.02.2016 15:43, Tofu Ninja wrote:
 Foreach seems to work if there is an opIndex() with no arguments that
 returns a range interface, is this documented? I can't seem to find
 anything that say this is supposed to happen. I am not really
 complaining, its nice, but I just didnt really expect it because I feel
 like I remember this being an error some time ago.
https://issues.dlang.org/show_bug.cgi?id=14619
Feb 06 2016
parent anonymous <anonymous example.com> writes:
On 06.02.2016 16:00, anonymous wrote:
 https://issues.dlang.org/show_bug.cgi?id=14619
Sorry, posted a bit hastily. Issue 14619 is just about a bug in the feature. The more relevant issue is number 5605: https://issues.dlang.org/show_bug.cgi?id=5605 As far as I can tell, this is not documented on dlang.org. It should be, of course. I personally think it's a misfeature. Explicit slicing is just two characters longer and makes it more clear what's happening.
Feb 06 2016
prev sibling parent reply "H. S. Teoh via Digitalmars-d-learn" <digitalmars-d-learn puremagic.com> writes:
On Sat, Feb 06, 2016 at 02:43:52PM +0000, Tofu Ninja via Digitalmars-d-learn
wrote:
 Foreach seems to work if there is an opIndex() with no arguments that
 returns a range interface, is this documented? I can't seem to find
 anything that say this is supposed to happen. I am not really
 complaining, its nice, but I just didnt really expect it because I
 feel like I remember this being an error some time ago.
Not really sure, but opIndex() with no arguments is supposed to be the current way of implement the [] slicing operator for user-defined types. I'm not sure when foreach started supporting that, but it's certainly a nice thing! T -- Nobody is perfect. I am Nobody. -- pepoluan, GKC forum
Feb 06 2016
next sibling parent reply ZombineDev <valid_email he.re> writes:
On Saturday, 6 February 2016 at 15:02:16 UTC, H. S. Teoh wrote:
 On Sat, Feb 06, 2016 at 02:43:52PM +0000, Tofu Ninja via 
 Digitalmars-d-learn wrote:
 Foreach seems to work if there is an opIndex() with no 
 arguments that returns a range interface, is this documented? 
 I can't seem to find anything that say this is supposed to 
 happen. I am not really complaining, its nice, but I just 
 didnt really expect it because I feel like I remember this 
 being an error some time ago.
Not really sure, but opIndex() with no arguments is supposed to be the current way of implement the [] slicing operator for user-defined types. I'm not sure when foreach started supporting that, but it's certainly a nice thing! T
I thought that opSlice() was supposed to be that operator. At least this is what's used in std.container (e.g.
Feb 06 2016
parent "H. S. Teoh via Digitalmars-d-learn" <digitalmars-d-learn puremagic.com> writes:
On Sat, Feb 06, 2016 at 03:11:22PM +0000, ZombineDev via Digitalmars-d-learn
wrote:
 On Saturday, 6 February 2016 at 15:02:16 UTC, H. S. Teoh wrote:
On Sat, Feb 06, 2016 at 02:43:52PM +0000, Tofu Ninja via
Digitalmars-d-learn wrote:
Foreach seems to work if there is an opIndex() with no arguments
that returns a range interface, is this documented? I can't seem to
find anything that say this is supposed to happen. I am not really
complaining, its nice, but I just didnt really expect it because I
feel like I remember this being an error some time ago.
Not really sure, but opIndex() with no arguments is supposed to be the current way of implement the [] slicing operator for user-defined types. I'm not sure when foreach started supporting that, but it's certainly a nice thing! T
I thought that opSlice() was supposed to be that operator. At least this is what's used in std.container (e.g.
That's the old use of opSlice(), which is still supported for backward compatibility. The new use of opSlice, after Kenji's multidimensional array PR, is to translate x..y notation into a type that opIndex can understand. Namely, this: x[1, 2..3, 4, 5..6] is translated into: x.opIndex(1, x.opSlice(2,3), 4, x.opSlice(5,6)) T -- I am Ohm of Borg. Resistance is voltage over current.
Feb 06 2016
prev sibling parent Tofu Ninja <emmons0 purdue.edu> writes:
On Saturday, 6 February 2016 at 15:02:16 UTC, H. S. Teoh wrote:
 Not really sure, but opIndex() with no arguments is supposed to 
 be the current way of implement the [] slicing operator for 
 user-defined types. I'm not sure when foreach started 
 supporting that, but it's certainly a nice thing!


 T
It feels a little weird because none of the range functions like map support calling opIndex so you can omit the [] in foreach but not with map. Just feels inconsistent.
Feb 06 2016