Thread overview | ||||||||
---|---|---|---|---|---|---|---|---|
|
May 21, 2011 Some help on Mixin Template at Class level. | ||||
---|---|---|---|---|
| ||||
Hi,
As the documentation at D ONLY shows template at functions level and also ONLY the content of a class but without the definition of a class.
Could this code be working? Or did I miss out some syntax.
mixin template AType(alias T, U, alias V){
class T : ClassC { // Class level Template
private:
U value;
public:
this(){}
void print(){}
mixin V;
} // End Class
}
class ClassC {}
mixin template btype() {
void someFunction() {}; // Content of a class.
}
mixin AType!("ClassB", string, btype);
void main() {
ClassC r = new ClassB();
}
-------- Build Commands: --------
-od"bin"
-of"bin\TestTemplate.exe"
-I"src"
"src\Sample.d"
src\Sample.d(20): Error: undefined identifier ClassB, did you mean class ClassC?
src\Sample.d(20): Error: ClassB is used as a type
Thanks very much for helping out.
--
Matthew Ong
email: ongbp@yahoo.com
|
May 21, 2011 Re: Some help on Mixin Template at Class level. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Matthew Ong | On Sat, 21 May 2011 10:54:54 +0200, Matthew Ong <ongbp@yahoo.com> wrote: > mixin template AType(alias T, U, alias V){ > class T : ClassC { // Class level Template This gives you a class called T. You seem to want it to have the name you pass as a string, in which case you have to use string mixins. > private: > U value; > public: > this(){} > void print(){} > mixin V; > } // End Class > } > > class ClassC {} > > mixin template btype() { > void someFunction() {}; // Content of a class. > > } > > mixin AType!("ClassB", string, btype); > > void main() { > ClassC r = new ClassB(); > } As mentioned above, in order to use the name from the template parameter you need to use string mixins. Here is how I would do it: string ATypeGenerator( string name ) { return "class " ~ name ~ " : ClassC { private: U value; public: this(){} void print(){} mixin V; }"; } mixin template AType( string name, U, alias V ) { mixin( ATypeGenerator( name ) ); } mixin AType!("ClassB", string, btype); void main() { ClassC r = new ClassB(); } -- Simen |
May 21, 2011 Re: Some help on Mixin Template at Class level. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Simen Kjaeraas | On 5/21/2011 5:12 PM, Simen Kjaeraas wrote: > On Sat, 21 May 2011 10:54:54 +0200, Matthew Ong <ongbp@yahoo.com> wrote: > >> mixin template AType(alias T, U, alias V){ >> class T : ClassC { // Class level Template > > This gives you a class called T. You seem to want it to have the name > you pass as a string, in which case you have to use string mixins. > >> private: >> U value; >> public: >> this(){} >> void print(){} >> mixin V; >> } // End Class >> } >> >> class ClassC {} >> >> mixin template btype() { >> void someFunction() {}; // Content of a class. >> >> } >> >> mixin AType!("ClassB", string, btype); >> >> void main() { >> ClassC r = new ClassB(); >> } > > As mentioned above, in order to use the name from the template parameter > you need to use string mixins. Here is how I would do it: > > string ATypeGenerator( string name ) { > return "class " ~ name ~ " : ClassC { > private: > U value; > public: > this(){} > void print(){} > mixin V; > }"; > } > > mixin template AType( string name, U, alias V ) { > mixin( ATypeGenerator( name ) ); > } > > mixin AType!("ClassB", string, btype); > > void main() { > ClassC r = new ClassB(); > } > > Hi, Thanks for the respond. I am really new to D. Using your code I have this error: src\Sample.d(16): Error: undefined identifier btype, did you mean template AType(string name,U,alias V)? src\Sample.d(16): Error: mixin AType!("ClassB",string,_error_) does not match template declaration AType(string name,U,alias V) Could you please ensure it is compilable code and so that I can test the template shown by you. -- Matthew Ong email: ongbp@yahoo.com |
May 22, 2011 Re: Some help on Mixin Template at Class level. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Matthew Ong | On Sat, 21 May 2011 11:40:22 +0200, Matthew Ong <ongbp@yahoo.com> wrote: > Using your code I have this error: > > src\Sample.d(16): Error: undefined identifier btype, did you mean template AType(string name,U,alias V)? > src\Sample.d(16): Error: mixin AType!("ClassB",string,_error_) does not match template declaration AType(string name,U,alias V) > > Could you please ensure it is compilable code and so that I can test the template shown by you. Sorry about that. string ATypeGenerator( string name ) { return "class " ~ name ~ " : ClassC { private: U value; public: this(){} void print(){} mixin V; }"; } mixin template AType( string name, U, alias V ) { mixin( ATypeGenerator( name ) ); } class ClassC {} mixin template btype() { void someFunction() {}; // Content of a class. } mixin AType!("ClassB", string, btype); void main() { ClassC r = new ClassB(); } -- Simen |
May 23, 2011 Re: Some help on Mixin Template at Class level. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Simen Kjaeraas | On 5/23/2011 2:17 AM, Simen Kjaeraas wrote: > On Sat, 21 May 2011 11:40:22 +0200, Matthew Ong <ongbp@yahoo.com> wrote: > >> Using your code I have this error: >> >> src\Sample.d(16): Error: undefined identifier btype, did you mean >> template AType(string name,U,alias V)? >> src\Sample.d(16): Error: mixin AType!("ClassB",string,_error_) does >> not match template declaration AType(string name,U,alias V) >> >> Could you please ensure it is compilable code and so that I can test >> the template shown by you. > > Sorry about that. > > > string ATypeGenerator( string name ) { > return "class " ~ name ~ " : ClassC { > private: > U value; > public: > this(){} > void print(){} > mixin V; > }"; > } > > mixin template AType( string name, U, alias V ) { > mixin( ATypeGenerator( name ) ); > } > > class ClassC {} > > mixin template btype() { > void someFunction() {}; // Content of a class. > } > > mixin AType!("ClassB", string, btype); > > void main() { > ClassC r = new ClassB(); > } > Thanks simen. I have solve that issue. Someone also pointed out: template mydef(string name){ } mixin(mydef!("abc")); The above are NOT shown up left hand side of that when we click on Language Reference http://www.digitalmars.com/d/2.0/lex.html And also here. http://www.digitalmars.com/d/2.0/comparison.html not the same as: mixin template mydeff(T){ ... } mixin mydeff!(string); Some centralized documentation is needed for D. -- Matthew Ong email: ongbp@yahoo.com |
May 23, 2011 Re: Some help on Mixin Template at Class level. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Matthew Ong | On 5/23/11, Matthew Ong <ongbp@yahoo.com> wrote: > Someone also pointed out: > > template mydef(string name){ > } > > mixin(mydef!("abc")); > > The above are NOT shown up left hand side of that when we click on Language Reference You're probably looking for this: http://www.digitalmars.com/d/2.0/mixin.html |
Copyright © 1999-2021 by the D Language Foundation