digitalmars.D.learn - Mixin template functions are ignored in struct
- tcak (24/24) Oct 14 2014 I have written a struct and a mixin template, and that mixin
- anonymous (11/39) Oct 14 2014 So, yes, the mixed in `apply` doesn't overload with the other one.
- tcak (5/53) Oct 14 2014 Hmm, so it is not able to mix enough then. That's a weird
- Adam D. Ruppe (23/26) Oct 14 2014 You could rename the method in the struct then mixin the rest.
- Adam D. Ruppe (6/8) Oct 14 2014 Nope, what happens is mixin stuff adds items by name. If you have
- Daniel N (17/19) Oct 14 2014 Use a normal mixin + token strings(q{}).
I have written a struct and a mixin template, and that mixin template is mixed into that struct as follows. private mixin template TestCommonMethods(){ public bool apply( int d, int e ){ return false; } } public struct Test{ public mixin TestCommonMethods; public bool apply( char c ){ return true; } } void main(){ Test t; t.apply( 5, 3 ); } --- For the line "t.apply( 5, 3 );", error is given saying that "function test.apply(char c) is not callable". --- For better testing, I added another function to template as "public bool blah(){}", and called it in main, and it works. So, thus this mean overloading is not supported with mixin templates?
Oct 14 2014
On Tuesday, 14 October 2014 at 20:58:19 UTC, tcak wrote:I have written a struct and a mixin template, and that mixin template is mixed into that struct as follows. private mixin template TestCommonMethods(){ public bool apply( int d, int e ){ return false; } } public struct Test{ public mixin TestCommonMethods; public bool apply( char c ){ return true; } } void main(){ Test t; t.apply( 5, 3 ); } --- For the line "t.apply( 5, 3 );", error is given saying that "function test.apply(char c) is not callable". --- For better testing, I added another function to template as "public bool blah(){}", and called it in main, and it works. So, thus this mean overloading is not supported with mixin templates?http://dlang.org/template-mixin.html says:If the name of a declaration in a mixin is the same as a declaration in the surrounding scope, the surrounding declaration overrides the mixin oneSo, yes, the mixed in `apply` doesn't overload with the other one. You can use an alias declaration to bring them together: public struct Test{ public mixin TestCommonMethods Common; alias apply = Common.apply; public bool apply( char c ){ return true; } }
Oct 14 2014
On Tuesday, 14 October 2014 at 21:10:02 UTC, anonymous wrote:On Tuesday, 14 October 2014 at 20:58:19 UTC, tcak wrote:Hmm, so it is not able to mix enough then. That's a weird decision though. Anyway, that suggested usage is making my work harder. I am putting that mixin in many struct and defining each method one by one in that way doesn't seem like suitable to me.I have written a struct and a mixin template, and that mixin template is mixed into that struct as follows. private mixin template TestCommonMethods(){ public bool apply( int d, int e ){ return false; } } public struct Test{ public mixin TestCommonMethods; public bool apply( char c ){ return true; } } void main(){ Test t; t.apply( 5, 3 ); } --- For the line "t.apply( 5, 3 );", error is given saying that "function test.apply(char c) is not callable". --- For better testing, I added another function to template as "public bool blah(){}", and called it in main, and it works. So, thus this mean overloading is not supported with mixin templates?http://dlang.org/template-mixin.html says:If the name of a declaration in a mixin is the same as a declaration in the surrounding scope, the surrounding declaration overrides the mixin oneSo, yes, the mixed in `apply` doesn't overload with the other one. You can use an alias declaration to bring them together: public struct Test{ public mixin TestCommonMethods Common; alias apply = Common.apply; public bool apply( char c ){ return true; } }
Oct 14 2014
On Tuesday, 14 October 2014 at 21:21:33 UTC, tcak wrote:Anyway, that suggested usage is making my work harder. I am putting that mixin in many struct and defining each method one by one in that way doesn't seem like suitable to me.You could rename the method in the struct then mixin the rest. Like private mixin template TestCommonMethods(){ public bool apply( int d, int e ){ return false; } } public struct Test{ public mixin TestCommonMethods; public bool apply2( char c ){ // now named apply2 return true; } } void main(){ Test t; t.apply( 5, 3 ); // works t.apply2('c'); // also works } The mixin template might also define an apply function that just forwards the call to the other name, similarly to how a final method in a class or interface might call a virtual function to allow customization.
Oct 14 2014
On Tuesday, 14 October 2014 at 20:58:19 UTC, tcak wrote:So, thus this mean overloading is not supported with mixin templates?Nope, what happens is mixin stuff adds items by name. If you have a variable or function in the object with the same name, it overrides the mixin one entirely. This is really useful for customizing the behavior of a mixin by taking most but not all of its functions.
Oct 14 2014
On Tuesday, 14 October 2014 at 20:58:19 UTC, tcak wrote:I have written a struct and a mixin template, and that mixin template is mixed into that struct as follows.Use a normal mixin + token strings(q{}). enum TestCommonMethods = q{ public bool apply( int d, int e ){ return false; } }; public struct Test{ mixin(TestCommonMethods); public bool apply(char c){ return true; } } void main(){ Test t; t.apply( 5, 3 ); }
Oct 14 2014