[Issue 15264] New: opDispatch cannot implement range primitives

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Thu Oct 29 18:38:28 PDT 2015


https://issues.dlang.org/show_bug.cgi?id=15264

          Issue ID: 15264
           Summary: opDispatch cannot implement range primitives
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: druntime
          Assignee: nobody at puremagic.com
          Reporter: ryan at rcorre.net

If you use opDispatch to implement range primitives, isInputRange passes yet it
cannot be used in foreach loops:

---
import std.range;

struct R {
  auto vals = iota(0, 5);
  auto opDispatch(string s)() { return mixin("vals."~s); }

  // uncomment to pass:
  //auto front() { return vals.front; }
  //auto popFront() { return vals.popFront; }
  //auto empty() { return vals.empty; }
}

// R is an input range, but cannot be used in foreach:
static assert(isInputRange!R);                               // pass
static assert(__traits(compiles, { foreach(v ; R()) { } })); // fail
---

We either need to allow foreach via opDispatched range primitives or have
is*Range return false in this case.

Personally, I would like to see foreach work in this case, though there may be
concerns over 'accidental' range implementation:
http://forum.dlang.org/thread/tociqzmmryxyggzypmdk@forum.dlang.org

--


More information about the Digitalmars-d-bugs mailing list