Thread overview
[Issue 3508] New: hasLength is broken for length defined as function
Nov 14, 2009
Kyle Foley
Nov 14, 2009
Kyle Foley
May 02, 2010
Masahiro Nakagawa
November 14, 2009
http://d.puremagic.com/issues/show_bug.cgi?id=3508

           Summary: hasLength is broken for length defined as function
           Product: D
           Version: 2.036
          Platform: Other
        OS/Version: Linux
            Status: NEW
          Severity: normal
          Priority: P2
         Component: Phobos
        AssignedTo: nobody@puremagic.com
        ReportedBy: k-foley@onu.edu


--- Comment #0 from Kyle Foley <k-foley@onu.edu> 2009-11-14 12:00:35 PST ---
import std.stdio;

struct Test1 { ulong length(); }
struct Test2 { ulong length; }
struct Test3 { string length; }

/* Definition from std.range

template hasLength(R)
{
    enum bool hasLength = is(typeof(R.init.length) : ulong);
}
*/

template hasLength(R)
{
    enum bool hasLength = is(typeof({
        auto r = R.init;
        auto l = r.length;
        static assert( is(typeof(l) : ulong) );
    }()));
}

int main(string[] args)
{
    writeln( std.range.hasLength!(Test1) ); // false
    writeln( std.range.hasLength!(Test2) ); // true
    writeln( std.range.hasLength!(Test3) ); // false

    writeln( test.hasLength!(Test1) ); // true
    writeln( test.hasLength!(Test2) ); // true
    writeln( test.hasLength!(Test3) ); // false

    return 0;
}

---

I included my proposed fix as test.hasLength.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
November 14, 2009
http://d.puremagic.com/issues/show_bug.cgi?id=3508



--- Comment #1 from Kyle Foley <k-foley@onu.edu> 2009-11-14 12:04:01 PST ---
(In reply to comment #0)
> import std.stdio;
> 
> struct Test1 { ulong length(); }
> struct Test2 { ulong length; }
> struct Test3 { string length; }
> 
> ...

I accidentally left out a few lines at the top (can't edit my original post):

---

module test;

import std.range;

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


Masahiro Nakagawa <repeatedly@gmail.com> changed:

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


--- Comment #2 from Masahiro Nakagawa <repeatedly@gmail.com> 2010-05-02 10:04:17 PDT ---
*** This issue has been marked as a duplicate of issue 2873 ***

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