Bug 3875 – std.range.hasLength does not work if .length is defined inside a static if

Status
RESOLVED
Resolution
INVALID
Severity
minor
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2010-03-04T06:11:00Z
Last change time
2015-06-09T01:27:41Z
Assigned to
nobody
Creator
philippe.sigaud

Comments

Comment #0 by philippe.sigaud — 2010-03-04T06:11:53Z
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); } ----
Comment #1 by repeatedly — 2010-05-04T17:23:19Z
"static if" has nothing to do with this issue. length method should be a property.