[mercury-users] this appears to be a mixin

David Jeffery dgj at cs.mu.OZ.AU
Tue Jul 10 23:42:48 AEST 2001


On 10-Jul-2001, Ralph Becket <rbeck at microsoft.com> wrote:
> > From: Dave Slutzkin [mailto:dave_slutzkin at yahoo.com]
> > Sent: 10 July 2001 13:42
> > 
> > A mixin is in fact, AFAIK, a base class that provides
> > all the functionality (methods and data) for a child
> > class - the class does not (should not) override the
> > methods itself.  A mixin provides functionality that
> > is exactly similar in many different classes, by
> > providing each class with these methods.  The classes
> > know nothing about this functionality and do not need
> > to know anything about it.  Then other classes that
> > access the mixin functionality need only know that the
> > class is derived from the mixin.
> 
> Searching the web I've come up with about three different
> notions of mixin class and several seemingly garbled
> attempts at explanation...
> 
> One of the clearer descriptions suggesting mixin classes
> provide functionality can be found at
> http://www.csis.pace.edu/~bergin/patterns/multipleinheritance.html
> 
> However, this page suggests that they're more like interfaces:
> http://www.devx.com/free/mgznarch/vcdj/1998/augmag98/mixin1.asp

A mixin class is basically an abstract base class (interface) which is 
designed to be used in multiple inheritance with any concrete class.
The derived class ``mixes in'' the methods of the abstract class with those
of the other base class.

Basically, you take one implementation and extend it with an alternative
interface (and you tend to implement that interface in terms of the existing
implementation).

In Mercury as Ralph points out, you can't do implementation inheritance
directly --- you need to use another technique like delegation to achieve
the same effect.

You might do something mixin-like in Mercury like this:

--------------------------------------------------------------------------

:- typeclass c(T) where [
	pred m(T::in, int::out) is det,
	...
].

:- typeclass mixin(T) where [
	pred mixin_method(...) is ...,
].

:- type t ---> some [T] f(T, ...) => c(T).

	% The type `t' effectively inherits the implementation of
	% `c' from the type stored inside it.
:- instance c(t) where [
	m(f(X, ...), I) :- m(X, I),
	...
].

	% Provide an implementation of `mixin', probably mostly in terms
	% of the inherited implementation of `c'.
:- instance mixin(t) where [
	...
].


--------------------------------------------------------------------------

Hope that helps.

(This is getting spooky... another posting on something very close to what
I'm working on in my thesis right now...).


dgj
-- 
David Jeffery (dgj at cs.mu.oz.au) | If you want to build a ship, don't drum up 
PhD student,                    | people together to collect wood or assign 
Dept. of Comp. Sci. & Soft. Eng.| them tasks and work, but rather teach them 
The University of Melbourne     | to long for the endless immensity of the sea.
Australia                       | -- Antoine de Saint Exupery
--------------------------------------------------------------------------
mercury-users mailing list
post:  mercury-users at cs.mu.oz.au
administrative address: owner-mercury-users at cs.mu.oz.au
unsubscribe: Address: mercury-users-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-users-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------



More information about the users mailing list