change mixins

Bill Baxter wbaxter at gmail.com
Sun Feb 14 15:12:45 PST 2010


On Sun, Feb 14, 2010 at 1:31 PM, Walter Bright
<newshound1 at digitalmars.com> wrote:
> Right now, mixins are defined and used as:
>
>   template foo(T) { declarations... }
>
>   mixin foo!(int) handle;
>
> The proposal is to switch it around:
>
>   mixin template foo(T) { declarations... }
>
>   foo!(int) handle;
>
> to follow the notion that mixin templates are very different from regular
> templates, and that should be reflected in their definition rather than use.

I have had one use for templates that are both stand-alone and mixed
in -- that's collections of aliases and utility functions based on a
particular type.

For instance imagine you have a Mesh type and a template that teases
apart the different subtypes it uses (VectorT, NormalT, floatT,
VertexT, ColorT, etc).

That's useful as a standalone:
   MeshTypes!(MyMeshT).VectorT
But also mixed into some class that deals with meshes
   class MeshManipulator(MeshT) {  mixin MeshTypes!(MeshT); ... }

I also had a collection of geometry utility functions in a template
(templated on the point type) which I would both use directly, and mix
in to classes that did a lot of geometry.  I guess that seems a little
wasteful (creating instantiations of these same functions in multiple
classes), but it was darn convenient.   pointSegmentDistance(p,a,b) is
much nicer than GeomUtils!(PointT).pointSegmentDistance(p,a,b).   You
can alias GeomUtils!(PointT) to something shorter, but aside from
'mixin' there's no way to import that templated namespace into another
namespace.

--bb



More information about the Digitalmars-d mailing list