Mixin templates as virtual function add tool

Voitech via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Apr 8 05:04:54 PDT 2016


Hi, I'm unfortunately in D this construction is not possible to 
define:

class A(T){
     abstract void method(S)(T arg1,S arg2);
}

As adding S to function declaration makes it not virtual.

But we can add this kind of functionality using mixin templates

mixin template BaseMix(T,S){
    abstract void method(T arg1,S arg2);
}

class Base{
    mixin BaseMix!(int,int);
}

//we can create now implementation by creation of other mixin
mixin template ImplMix(T,S){
    override void method(T arg1,S arg2){
     import std.stdio;
     writeln(T.strinof," ",S.stringof);
     //some dummy implementation
    }
}
//and initiate the class

class Impl:Base{
    mixin ImplMix!(int,int);
}

OK but i don't want to write each argument to mixin templates 
invocations
like:
    mixin ImplMix!(int,int);
    mixin ImplMix!(int,byte);
    mixin ImplMix!(int,string);
    mixin ImplMix!(int,Object);
    mixin ImplMix!(int,ubyte); etc.
It may produce a bug susceptible code as i would have to write it 
in two class definitions.

I created template that automatically calls mixins with PList... 
passed but i can't call it with mixins having more than one 
template param like ImplMix(T,S)


template MixinTypeIterate(alias mixinTemplate,TList...){
	static if(TList.length>0){
		mixin mixinTemplate!(TList[0]);
		static if(TList.length>1){
			mixin MixinTypeIterate!(mixinTemplate,TList[1..$]);
		}
	}
	
	
}
how to alias mixin template to be able to pass it to this one ?

class BaseClass(T){
    protected alias Types=AliasSeq!(int,string,ubyte);
    private alias WrappedMix(S)=mixin Mix!(T,S); //not compiles - 
mixins are no t regular templates
    mixin MixinTypeIterate!(WrappedImplMix,Types);
}
class ImplClass(T){
    private alias WrappedMix(T)=mixin ImplMix!(T,S); //not 
compiles - mixins are no t regular templates
    mixin MixinTypeIterate!(WrappedImplMix,Types);
}

How to make it work?

	




More information about the Digitalmars-d-learn mailing list