[Issue 4284] New: empty string[] alias lacks .length in a template

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sun Jun 6 06:43:19 PDT 2010


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

           Summary: empty string[] alias lacks .length in a template
           Product: D
           Version: future
          Platform: x86
        OS/Version: Windows
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody at puremagic.com
        ReportedBy: bearophile_hugs at eml.cc


--- Comment #0 from bearophile_hugs at eml.cc 2010-06-06 06:43:17 PDT ---
This looks like a correct D2 program:


import std.string: split;
template Foo(alias items) {
    static if (items.length == 0)
        enum Foo = 1;
    else
        enum Foo = 2;
}
enum string[] items = split("");
static assert(Foo!(items) == 1);
void main() {}


But DMD v2.046 prints at compile-time:

test.d(3): Error: expression (null.length) == 0u is not constant or does not
evaluate to a bool
test.d(9): Error: template instance test.Foo!(items) error instantiating
test.d(9): Error: static assert  (2 == 1) is false

------------------------

To fix it you have to test for null too:


import std.string: split;
template Foo(alias items) {
    static if (items == null || items.length == 0)
        enum Foo = 1;
    else
        enum Foo = 2;
}
enum string[] items = split("");
static assert(Foo!(items) == 1);
void main() {}

-- 
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