Thread overview
[phobos] AutoImplement: Shouldn't it be a mixin template?
Sep 08, 2010
David Simcha
Sep 08, 2010
SK
Sep 08, 2010
David Simcha
Sep 08, 2010
Shin Fujishiro
Sep 08, 2010
David Simcha
September 07, 2010
  The AutoImplement functionality in std.typecons is currently a class.
This severely and unnecessarily limits its usefulness in the following
cases:

1.  You want to implement more than one interface automagically.

2.  You want to implement stuff automatically, but also add more to the class.  For example, maybe you want to add some members to the abstract base class before auto implementing, and maybe override some of the auto implemented functionality.  Of course you could define 3 levels of inheritance (the abstract class with all the extra members, the auto implemented stuff, and the class with stuff overridden) but this is horribly verbose.

3.  You want to evaluate the automatically implemented functionality in some scope other than std.typecons, so you can import stuff.

Shouldn't we just make AutoImplement a mixin template instead?  This can be done trivially by mixing the string mixin currently used to implement it into a mixin template's scope.  It seems like it would solve all the relevant problems.  Then we could do stuff like:

interface Interface1 {
     void fun();
     void gun();
}

interface Interface2 {
     void hun();
}

class Class : Interface1, Interface2 {
     mixin AutoImplement!(Interface1, generateEmptyFunction,
isAbstractFunction);
     mixin AutoImplement!(Interface2, generateEmptyFunction,
isAbstractFunction);

     // Override the automatic implementation of fun() taking advantage of
     // template mixin name lookup rules.
    override void fun() {
        writeln("Overridden.");
     }
}

The killer use case for this would be automatic forwarding for the decorator pattern.  You could just mix in an AutoImplement that forwards all of your decorator interface functions to the encapsulated object, and then override whichever ones you need to.
September 07, 2010
On Tue, Sep 7, 2010 at 8:54 PM, David Simcha <dsimcha at gmail.com> wrote:
> ?The AutoImplement functionality in std.typecons is currently a class. ?This severely and unnecessarily limits its usefulness in the following cases:
>

Hi - Being poorly versed in mixins, I mentally picture the mess of
preprocessor macro abuse.  Are mixins going to make phobos in this
case easier to reason about and debug?  Do I ever get to see (and step
though) the source that actually got compiled?
I'd like to feel better that mixins are on *much* friendlier turf than macros.

This probably belongs on the d.learn list, but this has been bugging for a while and you provided this opportunity to blurt out.  :^)
September 08, 2010
On Wed, Sep 8, 2010 at 2:07 AM, SK <sk at metrokings.com> wrote:

> On Tue, Sep 7, 2010 at 8:54 PM, David Simcha <dsimcha at gmail.com> wrote:
> >  The AutoImplement functionality in std.typecons is currently a class.
>  This
> > severely and unnecessarily limits its usefulness in the following cases:
> >
>
> Hi - Being poorly versed in mixins, I mentally picture the mess of preprocessor macro abuse.  Are mixins going to make phobos in this case easier to reason about and debug?


Yes, at least compared to the error-prone and verbose boilerplate code that they're intended to replace.


> Do I ever get to see (and step
> though) the source that actually got compiled?
>

No, but the whole point is that the code will be less buggy in the first place than manually written boilerplate code.


> I'd like to feel better that mixins are on *much* friendlier turf than macros.
>
>
They are.  For example, they have a concept of scope and work at the semantic, rather than the textual, level.  For example, you don't need to give mixins UGLY_ALL_CAPS_NAMES because they won't accidentally expand.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/phobos/attachments/20100908/7e5d3f82/attachment.html>
September 09, 2010
David Simcha <dsimcha at gmail.com> wrote:
> Shouldn't we just make AutoImplement a mixin template instead?  This can be done trivially by mixing the string mixin currently used to implement it into a mixin template's scope.  It seems like it would solve all the relevant problems.

I agree that there should be a mixin template.

Actually, at first I created AutoImplement as a mixin-able template. But it turned out that template mixins had problems with virtual functions, and I changed it to a class.


> class Class : Interface1, Interface2 {
>      mixin AutoImplement!(Interface1, generateEmptyFunction, isAbstractFunction);
>      mixin AutoImplement!(Interface2, generateEmptyFunction, isAbstractFunction);
> 
>      // Override the automatic implementation of fun() taking advantage of
>      // template mixin name lookup rules.
>     override void fun() {
>         writeln("Overridden.");
>      }
> }
>
> The killer use case for this would be automatic forwarding for the decorator pattern.  You could just mix in an AutoImplement that forwards all of your decorator interface functions to the encapsulated object, and then override whichever ones you need to.

Bug 2740 blocks such a use of template mixins.  In spite of being annotated as override, your fun() doesn't go into vtable!

Template Mixins do not work as advertised
  http://d.puremagic.com/issues/show_bug.cgi?id=2740

That said, I think we can make AutoImplement a template mixin.  With a
quick test (unit tests), there seems to be no problem to make it so
except for that particular limitation (bug 2740).


Shin
September 08, 2010
On Wed, Sep 8, 2010 at 1:11 PM, Shin Fujishiro <rsinfu at gmail.com> wrote:

> Template Mixins do not work as advertised
>  http://d.puremagic.com/issues/show_bug.cgi?id=2740
>
> That said, I think we can make AutoImplement a template mixin.  With a
> quick test (unit tests), there seems to be no problem to make it so
> except for that particular limitation (bug 2740).
>
>
> This is a nasty bug.  See my latest update to the report.  It appears to
only happen when calling the method from its interface handle, i.e. only the interface vtable is wrong.  This is probably why I never noticed it before.

In the meantime, we should do the right thing from a design perspective and just warn about that bug in the docs until it gets fixed.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/phobos/attachments/20100908/6a4c2d30/attachment.html>
January 02, 2011
That could be done like this:

class ShimClass : Interface1, Interface2 {}

alias AutoImplement!(ShimClass, ...) Class;

Also, I see there's no example of actually using AutoImplement. That means searching with Google and that page for AutoImplement will not find a simple and meaningful example. Shin?


Andrei

On 9/7/10 10:54 PM, David Simcha wrote:
> The AutoImplement functionality in std.typecons is currently a class. This severely and unnecessarily limits its usefulness in the following cases:
>
> 1. You want to implement more than one interface automagically.
>
> 2. You want to implement stuff automatically, but also add more to the class. For example, maybe you want to add some members to the abstract base class before auto implementing, and maybe override some of the auto implemented functionality. Of course you could define 3 levels of inheritance (the abstract class with all the extra members, the auto implemented stuff, and the class with stuff overridden) but this is horribly verbose.
>
> 3. You want to evaluate the automatically implemented functionality in some scope other than std.typecons, so you can import stuff.
>
> Shouldn't we just make AutoImplement a mixin template instead? This can be done trivially by mixing the string mixin currently used to implement it into a mixin template's scope. It seems like it would solve all the relevant problems. Then we could do stuff like:
>
> interface Interface1 {
> void fun();
> void gun();
> }
>
> interface Interface2 {
> void hun();
> }
>
> class Class : Interface1, Interface2 {
> mixin AutoImplement!(Interface1, generateEmptyFunction,
> isAbstractFunction);
> mixin AutoImplement!(Interface2, generateEmptyFunction,
> isAbstractFunction);
>
> // Override the automatic implementation of fun() taking advantage of
> // template mixin name lookup rules.
> override void fun() {
> writeln("Overridden.");
> }
> }
>
> The killer use case for this would be automatic forwarding for the
> decorator pattern. You could just mix in an AutoImplement that forwards
> all of your decorator interface functions to the encapsulated object,
> and then override whichever ones you need to.
> _______________________________________________
> phobos mailing list
> phobos at puremagic.com
> http://lists.puremagic.com/mailman/listinfo/phobos