Implement a class with mixins

Don Clugston dac at nospam.com.au
Tue May 9 02:10:59 PDT 2006


Sean Kelly wrote:
> Don Clugston wrote:
>>
>> In every case I've seen or dreamt up so far, a template is either 
>> intended to be a mixin, or not. Is that your experience too?
> 
> Yes.
> 
>> I suspect that 'mixin' should be specified in the template 
>> declaration, rather than just the instantiation.
> 
> Perhaps it should, though I'm hesitant to suggest a special declaration 
> syntax unless it offers something over the current approach.

Me too, but I feel that mixins ought to be much more useful than they 
currently are. They don't feel very disciplined.

I can see two kinds of mixins:
(a) 'in' mixins, which are just like normal templates, except that they 
can use variables/functions/etc from the instantiation scope. Their name 
is the only thing that needs to appear in the instantiation scope.
(b) 'out' mixins, which can dump arbitrary declarations into the 
instantation scope.

The second type are far more scary, and so far I've found them less 
useful. You'd always want a 'mixin' keyword in front of any 
instantiation of them, because they could easily obfuscate code.
But I wonder if we really need it for the 'in' mixins.

I'm finding cases where I write:

int bar;
// mixin 'foo' creates a local function that requires a local integer 
called 'bar'.
mixin foo!(int) baz;
baz(45);

when actually I'd like to write

int bar;
foo!(int)(45);

I'm just bouncing around ideas, don't take it too seriously.



More information about the Digitalmars-d mailing list