Mixin template functions are ignored in struct

anonymous via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Oct 14 14:10:01 PDT 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 one

So, 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;
	}
}


More information about the Digitalmars-d-learn mailing list