Thread overview
Mixin Problems with D2.0
Mar 06, 2008
Brian White
Mar 06, 2008
Simen Kjaeraas
Mar 06, 2008
Brian White
Mar 06, 2008
Simen Kjaeraas
Mar 06, 2008
Bill Baxter
March 06, 2008
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
March 06, 2008
On Thu, 06 Mar 2008 12:02:24 +0100, Brian White <bcwhite@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)

-- Simen
March 06, 2008
> 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)

Got it.  Why not declare them "invariant char[]", or does it simply make no difference?

-- Brian
March 06, 2008
On Thu, 06 Mar 2008 13:32:57 +0100, Brian White <bcwhite@pobox.com> wrote:

>> 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)
>
> Got it.  Why not declare them "invariant char[]", or does it simply make no difference?
>
> -- Brian

Both char[] and invariant(char)[] are implicitly castable to const(char)[]. Had you for some reason done something like this:

template GenStruct(const char[] Name, const char[] M1)
{
  //stuffs
}

char[] myString1 = "Foo";
char[] myString2 = "bar";

GenStruct!(myString1, myString2); // error here, char[] not implicitly castable to invariant(char)[]

-- Simen
March 06, 2008
Simen Kjaeraas wrote:
> On Thu, 06 Mar 2008 12:02:24 +0100, Brian White <bcwhite@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