Mixin Problems with D2.0

Bill Baxter dnewsgroup at billbaxter.com
Thu Mar 6 10:57:34 PST 2008


Simen Kjaeraas wrote:
> On Thu, 06 Mar 2008 12:02:24 +0100, Brian White <bcwhite at pobox.com> wrote:
> 
>> I'm new to D and am playing with D2.0.  When I try to compile the 
>> example on http://www.digitalmars.com/d/2.0/mixin.html, I get a 
>> compiler error:
>>
>> --------------------------------------------------------------
>> template GenStruct(char[] Name, char[] M1)
>> {
>>      const char[] GenStruct = "struct " ~ Name ~ "{ int " ~ M1 ~ "; }";
>> }
>>
>> mixin(GenStruct!("Foo", "bar"));
>> --------------------------------------------------------------
>> testmixin.d(6): template instance GenStruct!("Foo","bar") does not 
>> match any template declaration
>> attribute argument to mixin must be a string, not 
>> (GetStruct!("Foo","bar"))
>> --------------------------------------------------------------
>>
>> Is there something else needed?
>>
>> -- Brian
> 
> 
> The problem is that "Foo" and "bar", as D strings, are 
> invariant(char)[], not char[].
> 
> The solution is to change the template to
> 
>> template GenStruct(const char[] Name, const char[] M1)

You can also use 'string' instead.

    template GenStruct(string Name, string M1)

which is an alias for invariant(char)[] in D2.

--bb


More information about the Digitalmars-d-learn mailing list