[Issue 18422] New: String members and parameters cannot be mixed in, even during CTFE

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sun Feb 11 15:22:36 UTC 2018


https://issues.dlang.org/show_bug.cgi?id=18422

          Issue ID: 18422
           Summary: String members and parameters cannot be mixed in, even
                    during CTFE
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: dmd
          Assignee: nobody at puremagic.com
          Reporter: andrei at erdani.com

This limits what can be done with CTFE and forces a variety of compile-time
data structures and functions to be complicated templates without necessity.

Consider a simple module introspection structure:

struct Module
{
    string name;
    string[] allMembers()
    {
        assert(__ctfe);
        mixin("immutable result = [ __traits(allMembers, " ~ name ~ ") ];");
        return result;
    }
}

unittest
{
    enum x = Module("std.typecons").allMembers;
}

The error is "value of this is not known at compile time". But it can, because
this is itself a compile-time value.

Passing the string as a parameter doesn't help either:

struct Module
{
    string[] allMembers(string name)
    {
        assert(__ctfe, "Can only be invoked during compilation");
        mixin("immutable result = [ __traits(allMembers, " ~ name ~ ") ];");
        return result;
    }
}

unittest
{
    enum x = Module().allMembers("std.typecons");
}

Here the error is "variable name cannot be read at compile time". Again that
should work because the code is being evaluated during compilation.

In this case the cure is simple - make Module a template parameterized on the
module name/alias. But the matter gets a lot more problematic when trying to
represent e.g. "all function declarations in this module".

--


More information about the Digitalmars-d-bugs mailing list