register mixin

Frank Benoit keinfarbton at nospam.xyz
Thu Apr 13 14:03:33 PDT 2006


A solution I though about was this:

/**/   class Speed : Signal!(int){
/**/     mixin Scaling   ( 0, 0, 1.23, "km/h") scale_kmh;
/**/     mixin NamedRange( 1, 100, 200, "HIGH SPEED" ) highSpeed;
/**/     mixin NamedValue( 2, 250, "MAX" ) maxSpeed;
/**/     mixin Impl      ( 3 );
/**/   }

One possiblity is to have and incrementing index in each mixin. Every
mixin creates a member variable containing the index. This can be done
with many static if:

static if( idx == 0 ){
	private Type getMixin0(){};
}
static if( idx == 1 ){
	private Type getMixin1(){};
}
static if( idx == 2 ){
	private Type getMixin2(){};
}
// ....

And than have an additional Impl mixin, which gets the count of
preceding mixins. It is implemented with a number of static if in the
constructor:

static if( count >= 1 ){
	list.append( getmMixin0() );
}
static if( count >= 2 ){
	list.append( getmMixin1() );
}
static if( count >= 3 ){
	list.append( getMixin2() );
}
// ....


Disadvantages:
* ugly
* upper limit for mixins
* manual indexing of the mixins + potential error




More information about the Digitalmars-d-learn mailing list