foreach seems to work with opIndex()

Tofu Ninja via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Feb 6 06:56:03 PST 2016


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[]) {}


More information about the Digitalmars-d-learn mailing list