www.digitalmars.com         C & C++   DMDScript  

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

reply d-bugmail puremagic.com writes:
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 puremagic.com
        ReportedBy: philippe.sigaud gmail.com



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: -------
Mar 04 2010
parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3875


Masahiro Nakagawa <repeatedly gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |repeatedly gmail.com
         Resolution|                            |INVALID



17:23:19 PDT ---
"static if" has nothing to do with this issue. length method should be a
property.

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