View mode: basic / threaded / horizontal-split · Log in · Help
May 16, 2004
Re: mixins
"Patrick Down" <pat@codemoon.com> wrote in message
news:Xns94EB98EDD61ACpatcodemooncom@63.105.9.61...
> This is pretty cool!
>
> A few question:
>
> Can you mixin that same mixin twice ( or more ) if
> you use a Mixin Identifier?

Yes.

> template foo(alias b)
> {
>   int abc() { return b; }
> }
>
> void test()
> {
>   int x = 8;
>   int y = 9;
>
>   mixin Foo!(x) XFoo;
>   mixin Foo!(y) YFoo;
>
>   assert(XFoo.abc() == 8);
>   assert(YFoo.abc() == 9);
> }
May 16, 2004
Re: mixins
"Carlos Santander B." <carlos8294@msn.com> wrote in message
news:c88o3c$5pf$1@digitaldaemon.com...
> I still fail to see the big picture, but since others have said it's a
good
> thing, then I guess they're right.
> One question: so a mixin, in some way, would be like an import?

The scoping and overriding rules are the same as for imports.
May 17, 2004
Re: mixins
Andy Friesen wrote:
> Singletons come to mind:

I'm planning on using mixins in Sinbad for exactly that purpose.  (See 
the proposal for it at dsource.)  And possibly quite a few others, such 
as the event system, which is something that had been giving me dizzy 
spells before.  These things could prove useful to no end...

-C. Sauls
-Invironz
May 17, 2004
Re: mixins
Aren't you missing a line, see below. :)
Regan.

On Sun, 16 May 2004 11:40:09 -0700, Andy Friesen <andy@ikagames.com> wrote:
>> [...] I want to see a kick-ass use for this, so I can write a
>> magazine article about it! A use that is cool, not easilly expressed in
>> other languages, and so would highlight the capabilities of D.
>>
>
> Singletons come to mind:
>
> singleton.d:
>
>      template Singleton(T) {
>          private static T _instance;
>          private static bool _deleted = false;
>
>          public static T get() {
>              assert(!_deleted);
>
>              if (_instance is null) {
>                  _instance = new T();
>              }
>              return _instance;
>          }
>
>          public static void destroy() {
>              assert(!_deleted);
>              delete _instance;
>              _instance = null;
//Line missing?
               deleted = true;
>          }
>      }
>
>      // best global symbol name ever
>      template the(T) {
>          T the() {
>              return T.get();
>          }
>      }
>
> main.d:
>
>      import singleton;
>
>      final class Foo {
>          mixin Singleton!(Foo);
>
>          private this() { ... }
>      }
>
>      int main() {
>          Foo theFoo = the!(Foo)();
>      }
>
> If you want to do the same in C++, you're pretty much stuck using the 
> preprocessor since you also have to deal with copy constructors and the 
> like.
>
> The only catch with the D singleton is that it's up to you to make the 
> constructor private.  (this could be alleviated by deferring 
> construction to a create() method, but that's stinky)
>
>   -- andy



-- 
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
May 17, 2004
Re: mixins
Regan Heath wrote:

> Aren't you missing a line, see below. :)
> Regan.

doh.  Yes.  The approach is sound, though.

 -- andy
May 17, 2004
Re: mixins
"Walter" <newshound@digitalmars.com> wrote in message news:c889f4$2io2$1@digitaldaemon.com...
> 
> "Hauke Duden" <H.NS.Duden@gmx.net> wrote in message
> news:c87go0$1f8j$2@digitaldaemon.com...
> > Walter wrote:
> >
> > > I have this mostly implemented. It's been based on a lot of your
> > > suggestions. The way I did it is, I think, pretty unexplored territory.
> > > That means there may be some pretty cool uses for it I've never thought of.
> > > Let me know what you think, and if I missed the boat completely <g>.
> > >
> > > www.digitalmars.com/d/mixin.html
> > >
> > >
> >
> > Oh yeah, forgot to mention: THANKS very much for implementing this! I
> > think this is a kick-ass feature!
> 
> You're welcome! Now, I want to see a kick-ass use for this, so I can write a
> magazine article about it! A use that is cool, not easilly expressed in
> other languages, and so would highlight the capabilities of D.
> 

This is GREAT (8^)
I personally like the way you have handled scope of mixins, makes perfect sense to me.
BTW you missed one of the (! in the text and can i suggest a minor grammatical change (in bold);

A TemplateMixin can occur in declaration lists of modules, classes, structs, unions, and as a statement. The TemplateIdentifier refers to a TemplateDeclaration. If the TemplateDeclaration has no parameters, the mixin form that has no (!TemplateArgumentList) can be used. 
Unlike a template instantiation, a template mixin's body is evaluated within the scope where the mixin appears, not where the template declaration is defined. It is analogous to cutting and pasting the body of the template into the location of the mixin. It is useful for injecting parameterized 'boilerplate' code, as well as for creating templated nested functions, which is not possible with template instantiations. 

As an advocate of class aggregation, I believe your implementation has a very promising future in this area.
I also think that MIXIN's are D's answer to MI, and plan to pursue this with some examples.

Watch this space

fred
May 17, 2004
Re: mixins
Thanks, I updated with your fixes.
May 17, 2004
Re: mixins
On Sun, 16 May 2004 17:54:56 -0700, Andy Friesen <andy@ikagames.com> wrote:
> Regan Heath wrote:
>
>> Aren't you missing a line, see below. :)
>> Regan.
>
> doh.  Yes.  The approach is sound, though.

Yes, I only mentioned the mistake cos I reckon everyone has copied it and 
is about to use it!

-- 
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
May 17, 2004
Re: mixins
The mixin example overrides the definition of "x" within the function
"test()".

Doesn't this violate the D rule that a symbol can't be multiply defined
within a function?

Tony

"Walter" <newshound@digitalmars.com> wrote in message
news:c876sh$10r7$1@digitaldaemon.com...
> I have this mostly implemented. It's been based on a lot of your
> suggestions. The way I did it is, I think, pretty unexplored territory.
That
> means there may be some pretty cool uses for it I've never thought of. Let
> me know what you think, and if I missed the boat completely <g>.
>
> www.digitalmars.com/d/mixin.html
>
>
May 17, 2004
Re: mixins
"Tony" <talktotony@email.com> wrote in message
news:c89gd4$19nf$1@digitaldaemon.com...
> The mixin example overrides the definition of "x" within the function
> "test()".
>
> Doesn't this violate the D rule that a symbol can't be multiply defined
> within a function?

That rule is probably going to have to go away :-(
1 2 3 4
Top | Discussion index | About this forum | D home