Mixin overload sets

BLM768 blm768 at gmail.com
Wed Aug 14 10:35:27 PDT 2013


The current behavior of placing identically-named functions from 
separate mixins into separate overload sets works well most of 
the time, but there are cases when the functions should be in the 
same overload set, and the aliasing required to allow overloading 
can quickly get tedious and clutter the namespace where the 
mixins are used, especially if the mixins define multiple 
functions.

mixin template someMixin(Args ...) {
   void abc(Args) {};
   void def(int, Args);
}

struct HasMixins {
   mixin someMixin!int mixinInt;
   alias mixinInt.abc abc;
   alias mixinInt.def def;
   mixin someMixin!float mixinFloat;
   alias mixinFloat.abc abc;
   alias mixinFloat.def def;
   //and so on...
}

It would be nice if we had a more concise way to pull all of the 
duplicate functions into a single overload set. I can think of 
two ways to handle this:

1. Introduce a new syntax.

struct HasMixins {
   //One possible syntax:
   alias mixin someMixin!int this;
   alias mixin someMixin!float this;
   //etc.
}

HasMixins hm;
hm.abc(3f); //Would call HasMixins.abc(float)

2. Automatically place functions that come from different 
instances of the same mixin template into the same namespace.

struct HasMixins {
   mixin someMixin!int;
   mixin someMixin!float;
}

HasMixins hm;
hm.abc(3f); //Would call HasMixins.abc(float)

mixin template someOtherMixin {
   void abc() {};
}

struct HasOtherMixins {
   mixin someMixin!int;
   mixin someMixin!float;
   mixin someOtherMixin;
}

HasOtherMixins hm2;
hm2.abc(3f); //error

Thoughts?



More information about the Digitalmars-d mailing list