www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Mixin Problems with D2.0

reply Brian White <bcwhite pobox.com> writes:
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
Mar 06 2008
parent reply "Simen Kjaeraas" <simen.kjaras gmail.com> writes:
On Thu, 06 Mar 2008 12:02:24 +0100, Brian White <bcwhite pobox.com> wrot=
e:

 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 compil=
er =
 error:

 --------------------------------------------------------------
 template GenStruct(char[] Name, char[] M1)
 {
      const char[] GenStruct =3D "struct " ~ Name ~ "{ int " ~ M1 ~ "; =
}";
 }

 mixin(GenStruct!("Foo", "bar"));
 --------------------------------------------------------------
 testmixin.d(6): template instance GenStruct!("Foo","bar") does not mat=
ch =
 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
Mar 06 2008
next sibling parent reply Brian White <bcwhite pobox.com> writes:
 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
Mar 06 2008
parent "Simen Kjaeraas" <simen.kjaras gmail.com> writes:
On Thu, 06 Mar 2008 13:32:57 +0100, Brian White <bcwhite pobox.com> wrot=
e:

 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 ma=
ke =
 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 =3D "Foo"; char[] myString2 =3D "bar"; GenStruct!(myString1, myString2); // error here, char[] not implicitly = castable to invariant(char)[] -- Simen
Mar 06 2008
prev sibling parent Bill Baxter <dnewsgroup billbaxter.com> writes:
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
Mar 06 2008