D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 4284 - empty string[] alias lacks .length in a template
Summary: empty string[] alias lacks .length in a template
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P2 normal
Assignee: No Owner
URL:
Keywords: patch, rejects-valid
Depends on:
Blocks:
 
Reported: 2010-06-06 06:43 UTC by bearophile_hugs
Modified: 2015-06-09 05:10 UTC (History)
3 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description bearophile_hugs 2010-06-06 06:43:17 UTC
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() {}