foreach seems to work with opIndex()

ZombineDev via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Feb 6 07:11:22 PST 2016


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. 
http://dlang.org/phobos/std_container_array.html#.Array.opSlice).


More information about the Digitalmars-d-learn mailing list