Supporting and signature-checking all foreach variations

Ashish Myles marcianx at gmail.com
Sun Feb 26 17:20:37 PST 2012


On Sun, Feb 26, 2012 at 5:25 AM, Ali Çehreli <acehreli at yahoo.com> wrote:
> On 02/25/2012 08:25 AM, Ashish Myles wrote:
>>    However the code above doesn't seem to work and requires me to
>>    explicitly invoke the slice operator myself like
>>      foreach(p; C[]) { ... }
>>    when my data structure clearly defines the following functions.
>>      Point3[] opSlice() { return _cpts[]; }
>>      const (Point3)[] opSlice() const { return _cpts[]; }
>>    Is this a misunderstanding on my part or an unimplemented feature?
>
> But I've just verified that the following works with dmd 2.058:
>
> import std.stdio;
>
> struct Point3
> {}
>
> struct MyCollection
> {
>    Point3[] _cpts;
>
>    Point3[] opSlice() { return _cpts; }  // <-- _cpts[] works too
>
>    const (Point3)[] opSlice() const { return _cpts; }
> }
>
> void main()
> {
>    auto coll = MyCollection();
>
>    foreach (i; coll) {
>        // ...
>    }
> }
>

Oh good to know. I had downgraded my dmd after some new CTFE bugs
resulted in compilation errors on some of my code, and hadn't thought
to check with the latest version.


More information about the Digitalmars-d-learn mailing list