Template mixins: Why is the decision to mixin made at the call site?

Bill Baxter wbaxter at gmail.com
Fri Aug 21 10:58:15 PDT 2009


On Fri, Aug 21, 2009 at 10:36 AM, div0<div0 at users.sourceforge.net> wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Chad J wrote:
>> Regarding template mixins, I'm curious, why is the decision to mixin a
>> template made at the call site and not at the declaration of the
>> template/mixin?
>>
>> In other words, why do we write
>>
>>       template foo()
>>       {
>>               // etc..
>>       }
>>
>>       mixin foo!();
>>
>> instead of
>>
>>       mixin foo()
>>       {
>>               // etc..
>>       }
>>
>>       foo!();
>>
>> ??
>>
>> It seems to me like in most cases you determine whether or not you want
>> to mixin based on the contents of the template, not based on the the
>> calling code.  Not to mention, declaring as a mixin would allow us to
>> omit that mixin keyword anytime we want to do something cool with
>> template mixins.  So it seems odd to me as I reflect on it.
>
> Not sure what the original choice was based on, but what you suggest
> looks wrong to me. You aren't necessarily using a template in order to
> mix it in somewhere.
>
> With that syntax it looks like you can only use foo as a mixin. If you
> change 'mixin' to 'paste' your way round looks even more wrong.

At least for templates that are just a bunch of alias declarations, it
can be handy to be able to use directly or to mix in.  So there would
be little to be gained by forcing the developer to choose "mixin" or
"regular template" up front.

For example

template MeshTypes(MeshType) {
   alias MeshType mesh_type
   alias typeof(MeshType.vertices[0]) vert_type;
   alias typeof(MeshType.vertices[0].x)  float_type;
   const uint vert_dim = vert_type.length;
}

That's handy to use inline like   MeshTypes!(MyMesh).vert_type
Or you can mix it in to another class that uses mesh a lot to have all
those types defined inside your class too.

--bb


More information about the Digitalmars-d-learn mailing list