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

Chad J chadjoan at __spam.is.bad__gmail.com
Sun Aug 23 13:35:59 PDT 2009


Bill Baxter wrote:
> On Fri, Aug 21, 2009 at 10:36 AM, div0<div0 at users.sourceforge.net> wrote:
>> -----BEGIN PGP SIGNED MESSAGE-----
>> Hash: SHA1
>>
>> 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

Alright, good example.

But then why not:

	mixin 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;
	}

	template MeshTypesOf(MeshType) {
	    mixin MeshTypes!(MeshType);
	}

Admittedly it is not as clean and violates DRY some.  But it does allow
the declarer to explicitly determine how the template/mixin gets used.

Eh, I was kinda hoping for a reason better than "there are some cases
where you want both a mixin and a template to be the same thing."  I
thought it might be something obvious to someone; something that I
missed.  Nonetheless, the template/mixin duality seems fairly benign, so
this decision is starting to look somewhat arbitrary to me.


More information about the Digitalmars-d-learn mailing list