Jump to page: 1 2
Thread overview
Template mixins: Why is the decision to mixin made at the call site?
Aug 21, 2009
Chad J
Aug 21, 2009
div0
Aug 21, 2009
div0
Aug 21, 2009
Ary Borenszweig
Aug 21, 2009
div0
Aug 21, 2009
Ary Borenszweig
Aug 22, 2009
div0
Aug 21, 2009
Bill Baxter
Aug 23, 2009
Chad J
August 21, 2009
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.
August 21, 2009
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.

- --
My enormous talent is exceeded only by my outrageous laziness.
http://www.ssTk.co.uk
August 21, 2009
On Fri, Aug 21, 2009 at 1:36 PM, div0<div0@users.sourceforge.net> wrote:
> 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.

That's what he's suggesting, and it does make sense.  When you write a template, *either* it's meant to be used as a mixin, *or* it's meant to be used some other way.  Mixin in a template that wasn't meant to be a mixin or vice versa usually makes no sense.

> If you
> change 'mixin' to 'paste' your way round looks even more wrong.

If you're thinking of 'mixin' as a verb, "to mix in", I can understand that.  But if you consider his syntax with "mixin" as a noun (like "class" or "template"), it makes more sense.
August 21, 2009
Jarrett Billingsley wrote:
> On Fri, Aug 21, 2009 at 1:36 PM, div0<div0@users.sourceforge.net> wrote:
>
> That's what he's suggesting, and it does make sense.  When you write a template, *either* it's meant to be used as a mixin, *or* it's meant to be used some other way.  Mixin in a template that wasn't meant to be a mixin or vice versa usually makes no sense.

Hmmm.

Not convinced by that argument, I can think of good reasons to use a template as both.

- --
My enormous talent is exceeded only by my outrageous laziness.
http://www.ssTk.co.uk
August 21, 2009
On Fri, Aug 21, 2009 at 10:36 AM, div0<div0@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
August 21, 2009
div0 wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
> 
> Jarrett Billingsley wrote:
>> On Fri, Aug 21, 2009 at 1:36 PM, div0<div0@users.sourceforge.net> wrote:
>>
>> That's what he's suggesting, and it does make sense.  When you write a
>> template, *either* it's meant to be used as a mixin, *or* it's meant
>> to be used some other way.  Mixin in a template that wasn't meant to
>> be a mixin or vice versa usually makes no sense.
> 
> Hmmm.
> 
> Not convinced by that argument, I can think of good reasons to use a
> template as both.

Can you provide an example?
August 21, 2009
Ary Borenszweig wrote:
> div0 wrote:
>> -----BEGIN PGP SIGNED MESSAGE-----
>> Hash: SHA1
>>
>> Jarrett Billingsley wrote:
>>> On Fri, Aug 21, 2009 at 1:36 PM, div0<div0@users.sourceforge.net> wrote:
>>>
>>> That's what he's suggesting, and it does make sense.  When you write a template, *either* it's meant to be used as a mixin, *or* it's meant to be used some other way.  Mixin in a template that wasn't meant to be a mixin or vice versa usually makes no sense.
>>
>> Hmmm.
>>
>> Not convinced by that argument, I can think of good reasons to use a template as both.
> 
> Can you provide an example?

Yeah, but it's Friday night, I'm drinking beer now. :o
I'll do it tomorrow.

Bills response is the start of it though.

- --
My enormous talent is exceeded only by my outrageous laziness.
http://www.ssTk.co.uk
August 21, 2009
div0 wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
> 
> Ary Borenszweig wrote:
>> div0 wrote:
>>> -----BEGIN PGP SIGNED MESSAGE-----
>>> Hash: SHA1
>>>
>>> Jarrett Billingsley wrote:
>>>> On Fri, Aug 21, 2009 at 1:36 PM, div0<div0@users.sourceforge.net> wrote:
>>>>
>>>> That's what he's suggesting, and it does make sense.  When you write a
>>>> template, *either* it's meant to be used as a mixin, *or* it's meant
>>>> to be used some other way.  Mixin in a template that wasn't meant to
>>>> be a mixin or vice versa usually makes no sense.
>>> Hmmm.
>>>
>>> Not convinced by that argument, I can think of good reasons to use a
>>> template as both.
>> Can you provide an example?
> 
> Yeah, but it's Friday night, I'm drinking beer now. :o
> I'll do it tomorrow.
> 
> Bills response is the start of it though.

Ah, it's ok, I saw Bill's answer later.
August 21, 2009
On Fri, 21 Aug 2009 13:54:38 -0400, div0 <div0@users.sourceforge.net> wrote:

> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Jarrett Billingsley wrote:
>> On Fri, Aug 21, 2009 at 1:36 PM, div0<div0@users.sourceforge.net> wrote:
>>
>> That's what he's suggesting, and it does make sense.  When you write a
>> template, *either* it's meant to be used as a mixin, *or* it's meant
>> to be used some other way.  Mixin in a template that wasn't meant to
>> be a mixin or vice versa usually makes no sense.
>
> Hmmm.
>
> Not convinced by that argument, I can think of good reasons to use a
> template as both.
>

What you could have is similar to scope classes, that is, if you define a template as a mixin template, it's always meant to be a mixin (which is a common case).

Now, scope classes must be declared as scope when used, so does it make sense to require mixin templates to be called via mixin?  I'm not sure, it would be against what the O.P. desired, but seeing the mixin keyword at the usage site is a huge documentation hint.

I do see value in declaring a template as a mixin template, making it an error to use it as a normal template.

-Steve
August 22, 2009
Steven Schveighoffer wrote:
> On Fri, 21 Aug 2009 13:54:38 -0400, div0 <div0@users.sourceforge.net> wrote:
> 
>> -----BEGIN PGP SIGNED MESSAGE-----
>> Hash: SHA1
>>
>> Jarrett Billingsley wrote:
>>> On Fri, Aug 21, 2009 at 1:36 PM, div0<div0@users.sourceforge.net> wrote:
>>>
>>> That's what he's suggesting, and it does make sense.  When you write a template, *either* it's meant to be used as a mixin, *or* it's meant to be used some other way.  Mixin in a template that wasn't meant to be a mixin or vice versa usually makes no sense.
>>
>> Hmmm.
>>
>> Not convinced by that argument, I can think of good reasons to use a template as both.
>>
> 
> What you could have is similar to scope classes, that is, if you define a template as a mixin template, it's always meant to be a mixin (which is a common case).
> 
> Now, scope classes must be declared as scope when used, so does it make sense to require mixin templates to be called via mixin?  I'm not sure, it would be against what the O.P. desired, but seeing the mixin keyword at the usage site is a huge documentation hint.
> 
> I do see value in declaring a template as a mixin template, making it an error to use it as a normal template.
> 
> -Steve

That would be nice.

You can sort of bodge that at the mo.
Just stick a const method in the template, then if you try and use it
outside a class/struct the compiler barfs.

Though really that needs to change; it's inconsistent.

- --
My enormous talent is exceeded only by my outrageous laziness.
http://www.ssTk.co.uk
« First   ‹ Prev
1 2