[Issue 3875] New: std.range.hasLength does not work if .length is defined inside a static if

d-bugmail at puremagic.com d-bugmail at puremagic.com
Thu Mar 4 06:11:54 PST 2010


http://d.puremagic.com/issues/show_bug.cgi?id=3875

           Summary: std.range.hasLength does not work if .length is
                    defined inside a static if
           Product: D
           Version: 2.040
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: minor
          Priority: P2
         Component: Phobos
        AssignedTo: nobody at puremagic.com
        ReportedBy: philippe.sigaud at gmail.com


--- Comment #0 from Philippe Sigaud <philippe.sigaud at gmail.com> 2010-03-04 15:11:53 CET ---
std.range.hasLength does not work if the .length member is defined inside a
static if. It's a common case for ranges wrapping other ranges and trying to
expose their input's properties:
---
static if (hasLength!Input)
    int length() { return _input.length;}
----
for example.


Here is some code demonstrating the problem:

----
import std.range;

struct Lengthy(bool i) {
    int front() { return 1;}
    void popFront();
    static if (i)
        int length() { return 1;}
}

void main() {
    Lengthy!true i;
    Lengthy!false ni;
    assert(hasLength!(typeof(i))); // Error: AssertError. Considers i has no
.length defined.
    assert(!hasLength!(typeof(ni)));
}
----

And a possible solution that seems to work quite well in practice:
----
template hasLength(R) {
    enum bool hasLength = __traits(compiles, R.length);
}
----

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list