Thread overview
Newbie confused: What is purpose of mixins?
Sep 04, 2004
Lynn Allan
Sep 04, 2004
Chris
Sep 04, 2004
Lynn Allan
Sep 04, 2004
Deja Augustine
Sep 04, 2004
Ben Hinkle
September 04, 2004
<alert comment="newbie">

I am unsure what problem or purpose "mixins" are designed to address. I've read the D specs and looked over numerous newsgroup postings. I've used C++ templates and multiple inheritance, and Java interfaces. Mixins seem related to these concepts, but it hasn't registered in my brain why I would use mixins. I can more or less grasp the syntax of declaring mixins with the example code, but I don't understand their purpose.

Are their previous postings or links to articles to give a fuller explanation to this ... in comprehensible English? (rather than excerpts from Phd papers ... "On one hand, the ontologies we develop during domain analysis tend to involve classes that inherit from multiple parents.")

Could the D spec page for mixins have links to such information about the purpose of mixins?

</alert>


September 04, 2004
> I am unsure what problem or purpose "mixins" are designed to address.

Sorry to post and have nothing valuable to contribute, but I was also having trouble grasping the purpose of mixins. A better explanation of mixins on the D web page would be helpful (since apparently mixins have different purposes for different languages).

Chris


September 04, 2004
Lynn Allan wrote:
> <alert comment="newbie">
> 
> I am unsure what problem or purpose "mixins" are designed to address. I've read the D specs and looked over numerous newsgroup postings. I've used C++ templates and multiple inheritance, and Java interfaces. Mixins seem related to these concepts, but it hasn't registered in my brain why I would use mixins. I can more or less grasp the syntax of declaring mixins with the example code, but I don't understand their purpose.
> 
> Are their previous postings or links to articles to give a fuller explanation to this ... in comprehensible English? (rather than excerpts from Phd papers ... "On one hand, the ontologies we develop during domain analysis tend to involve classes that inherit from multiple parents.")
> 
> Could the D spec page for mixins have links to such information about the purpose of mixins?
> 
> </alert> 
> 
> 


Someone more knowledgable please correct me if I'm wrong, but this is what I understand about mixins:

Mixins are designed to alter the scope of a template instance.  When you declare a template

# template foo()
# {
#    int x = 5;
# }

and instantiate it using

# foo!()

D creates a template scope nested inside of the scope in which the template is DECLARED.

A mixin on the other hand, directly imports the symbols generated by the template into the current scope of the mixin.

That's a bit PhD so I'll try to further explain with an example (taken directly from the D documentation on templates)

If you're using template instances, the following occurs:

	-------- module a ---------
	template TFoo(T) { void bar() { func(); } }

	-------- module b ---------
	import a;

	void func() { }
	alias TFoo!(int) f;	// error: func not defined in module a

	f.bar(); // added by me for demonstration purposes

However, that can be fixed by using a mixin like so:

	-------- module a ---------
	template TFoo(T) { void bar() { func(); } }

	-------- module b ---------
	import a;

	void func() { }
	mixin TFoo!(int);

	bar();

Let's take a closer look at what's happening in those two samples.  In the first sample, the template is being declared in module a and when an instance is created, it looks in module a (the scope of the template's declaration) for the func() function.  Since there is no func() in module a, it generates an error.  If it had worked, you can also see that you would need to address the template's scope first (done by an alias in this case) to access the bar method.

In the second example, when the mixin is called, it essentially copies and pastes the contents of the template in place of the mixin.  So, what the compiler actually sees once the mixin is evaluated is something like this:

	-------- module a ---------
	template TFoo(T) { void bar() { func(); } }

	-------- module b ---------
	import a;

	void func() { }
	void bar() { func(); } // was mixin TFoo!(int);

	bar();

As you can see, that's perfectly valid D code and does not generate any errors.

There's a lot more you can do with mixins and there are a few restrictions and warnings that it wouldn't hurt to read about, but this should at least help you to understand the difference between mixins and template instances in D.

Hope this helps,

-Deja
September 04, 2004
I looked at several websites, and am no less confused than before. My impression is that it might be useful for some types of semi-generic sorting? I've vaguely heard of "generic programming", but that is about all. I used some C++ MI, but didn't care for it except for "composition". I stayed away from C++ templates and STL because of the syntax and problematic error messages, but that could be another case of ... "a poor craftman blames his tools."

One thing I've wondered about "Interfaces": there seems to be potential for duplicate code, and perhaps mixin's could improve that?

Everything else about D has been quite "real world" and applicable, so I hesitate to ignore it as merely of academic interest.


"Chris" <ctlajoie@hotmail.com> wrote in message news:chcion$2r0c$1@digitaldaemon.com...
> > I am unsure what problem or purpose "mixins" are designed to address.
>
> Sorry to post and have nothing valuable to contribute, but I was also
having
> trouble grasping the purpose of mixins. A better explanation of mixins on the D web page would be helpful (since apparently mixins have different purposes for different languages).
>
> Chris
>
>


September 04, 2004
you can think of it as a sort of .. intelligent, type-safe macro.  but only sort of.

mixins are expanded (inlined) when they are used, so you can use it to insert code.

void PrintMessage() { writefln("message!"); }

template Fork { PrintMessage(); }

void main()
{
    mixin Fork;
}

a pretty useless example, but it will call PrintMessage() when you use mixin Fork.   it's because it expands inline so the mixin Fork line in main() becomes:

    PrintMessage();

the more interesting stuff you can do with a mixin is when you use parameters.

template Spoon(T) { T num; }

void main()
{
    mixin Spoon!(float);
    num=5.5;
}

personally i don't use them that often and i think they are of limited usefulness.. but they're there.  and they're interesting.

September 04, 2004
In article <chceb0$2omh$1@digitaldaemon.com>, Lynn Allan says...
>
><alert comment="newbie">
>
>I am unsure what problem or purpose "mixins" are designed to address. I've read the D specs and looked over numerous newsgroup postings. I've used C++ templates and multiple inheritance, and Java interfaces. Mixins seem related to these concepts, but it hasn't registered in my brain why I would use mixins. I can more or less grasp the syntax of declaring mixins with the example code, but I don't understand their purpose.

I used mixins in MinTL as a way of sharing code between two struct types. With a
class one can share code by having the two classes subclass a common base class
but with structs there isn't any subclassing so the alternative is to write the
common functions as a template and mix in the template into the two structs. I
also tried using mixins with the std.stream.Stream to factor out the default
implementations of input and output functions (ie - a Stream implements the
InputStream interface and the OutputStream interface by mixing in the
DefaultInputStream and DefaultOutputStream templates) but that didn't go as well
since they involved private declarations and it looks like private declarations
in a mixin are really private to the template so it was getting messy and I put
that on the back burner. I'm sure mixins will be useful for stream, but I just
don't know about how it will actually look.
I'm sure there are plenty of other "real world" examples but those are two place
were I have run into mixins.

-Ben