www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Automatic Interface Implementation

reply "JS" <js.mdnq gmail.com> writes:
I ripped and hacked up some code from typecons that allows one to 
implement an interface automatically by redirecting it to a 
variable implementing the interface.

http://dpaste.dzfl.pl/209e260b

To see the effectiveness of the code, notice that very little, if 
any, boilerplate code needs to be used to implement such a 
pattern:


interface A
{
	void foo();
	void foo2();
	int bar(int x);
	void bar(string c);
}

// Dummy class for A
class AA : A { void foo() { writeln("AA.foo");  } void foo2() { 
writeln("AA.foo2"); } int bar(int x) { writeln("AA.bar(int)"); 
return 0; }  void bar(string c) { writeln("AA.bar(string)"); } }

class B : A
{
	A a;

	void foo2() { writeln("B.foo2"); }
	mixin implementComposite!a;
}


I'm not sure how this code holds up though, it's possible 
templated classes and other more complex implementations may 
cause some problems....

It will probably do fine for what I need it to do but maybe 
someone else will find it useful or can make it better...
Jul 05 2013
parent reply "Vladimir Panteleev" <vladimir thecybershadow.net> writes:
On Friday, 5 July 2013 at 17:53:11 UTC, JS wrote:
 I ripped and hacked up some code from typecons that allows one 
 to implement an interface automatically by redirecting it to a 
 variable implementing the interface.
How about splitting up the implementation of AutoImplement into a template mixin, which would then be usable on its own? Some previous discussion on the topic: http://forum.dlang.org/post/20100909021137.40d2dce2.rsinfu gmail.com The new "wrap" from std.typecons looks like it might help, however: 1) The type it creates is completely inaccessible (the return type is the interface); 2) It doesn't seem to play well with "alias this". Instead of wrapping methods in the struct containing "alias this", it uses the methods (with the same name/signature) from the object "alias this" points to.
Jul 05 2013
next sibling parent reply "JS" <js.mdnq gmail.com> writes:
On Friday, 5 July 2013 at 22:33:01 UTC, Vladimir Panteleev wrote:
 On Friday, 5 July 2013 at 17:53:11 UTC, JS wrote:
 I ripped and hacked up some code from typecons that allows one 
 to implement an interface automatically by redirecting it to a 
 variable implementing the interface.
How about splitting up the implementation of AutoImplement into a template mixin, which would then be usable on its own? Some previous discussion on the topic: http://forum.dlang.org/post/20100909021137.40d2dce2.rsinfu gmail.com The new "wrap" from std.typecons looks like it might help, however: 1) The type it creates is completely inaccessible (the return type is the interface); 2) It doesn't seem to play well with "alias this". Instead of wrapping methods in the struct containing "alias this", it uses the methods (with the same name/signature) from the object "alias this" points to.
I took the code from the latest std.typecons wrap and modified it(well, after initially doing my own interface). I just needed a quick implementation. std.typecon has a lot of goodies in it but it seems to re-implement much of the same functionality in many of the templates. wrap did not compile do I took out the stuff that didn't work and hacked it up to get what I needed out of it.
Jul 05 2013
parent reply "Vladimir Panteleev" <vladimir thecybershadow.net> writes:
On Friday, 5 July 2013 at 22:43:06 UTC, JS wrote:
 std.typecon has a lot of goodies in it but it seems to 
 re-implement much of the same functionality in many of the 
 templates.
I agree, it would be nice if it would be broken up into smaller, reusable pieces that would also be usable from user code for tasks such as yours.
 wrap did not compile do I took out the stuff that didn't work 
 and hacked it up to get what I needed out of it.
Here is my attempt at using it: http://dump.thecybershadow.net/9562c8f705cb578be1e1513b6f931fb8/test.d However, calling b.foo2 prints AA.foo2 instead of B.foo2 due to the bug I mentioned.
Jul 05 2013
parent "JS" <js.mdnq gmail.com> writes:
On Friday, 5 July 2013 at 23:22:23 UTC, Vladimir Panteleev wrote:
 On Friday, 5 July 2013 at 22:43:06 UTC, JS wrote:
 std.typecon has a lot of goodies in it but it seems to 
 re-implement much of the same functionality in many of the 
 templates.
I agree, it would be nice if it would be broken up into smaller, reusable pieces that would also be usable from user code for tasks such as yours.
 wrap did not compile do I took out the stuff that didn't work 
 and hacked it up to get what I needed out of it.
Here is my attempt at using it: http://dump.thecybershadow.net/9562c8f705cb578be1e1513b6f931fb8/test.d However, calling b.foo2 prints AA.foo2 instead of B.foo2 due to the bug I mentioned.
Since I do not have wrap to try it out(Kenji suggested using it in another post which is when I started looking at the typecons source) and as far as I can tell it actually wraps a type rather than just implements functions for an interface, it is not exactly what I needed, but code wise, pretty close. I didn't need to create a new wrapper class but just mixin the implementations of an interface(although, in theory, one could also use it for abstract methods).
Jul 05 2013
prev sibling parent reply "John Colvin" <john.loughran.colvin gmail.com> writes:
On Friday, 5 July 2013 at 22:33:01 UTC, Vladimir Panteleev wrote:
 The new "wrap" from std.typecons
Yikes... I'm a native english speaker and pretty confident with D but I can't get my head around what wrap does. It looks suspiciously like the docs were written by a Russian (missing articles :) ). Any chance you could provide an explanation for me?
Jul 05 2013
next sibling parent "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Sat, Jul 06, 2013 at 12:54:37AM +0200, John Colvin wrote:
 On Friday, 5 July 2013 at 22:33:01 UTC, Vladimir Panteleev wrote:
The new "wrap" from std.typecons
Yikes... I'm a native english speaker and pretty confident with D but I can't get my head around what wrap does. It looks suspiciously like the docs were written by a Russian (missing articles :) ).
The docs could use actual code examples to show what it does, instead of relying on mere words. :-/ I'd say file a bug in the bugtracker if there isn't one already.
 Any chance you could provide an explanation for me?
I just read the docs, and I'm just as confused as you are. :-( T -- It is the quality rather than the quantity that matters. -- Lucius Annaeus Seneca
Jul 05 2013
prev sibling next sibling parent "Vladimir Panteleev" <vladimir thecybershadow.net> writes:
On Friday, 5 July 2013 at 22:54:38 UTC, John Colvin wrote:
 On Friday, 5 July 2013 at 22:33:01 UTC, Vladimir Panteleev 
 wrote:
 The new "wrap" from std.typecons
Yikes... I'm a native english speaker and pretty confident with D but I can't get my head around what wrap does. It looks suspiciously like the docs were written by a Russian (missing articles :) ). Any chance you could provide an explanation for me?
I agree, the docs are rather terse. I only know about it because quite a while ago, Kenji posted[1] some code including adaptTo. 3 years later, it made it into Phobos. What it does, is: Let's say you have a struct S, and an interface I, which both have (some of) the same methods. You'd like to refer to an instance of S through the I interface. Now, a D interface is just a vtable, but structs can't have virtual methods or inheritance. To do that (by hand), you'd declare a class which inherits from I, contains a copy of S, and for each method in I, it implements it by forwarding the call to its copy of S. The wrap function template does this for you - it generates the class, instantiates it, initializes its copy of S, and returns an I that points to the new class instance. Feel free to send a pull request to improve the documentation. (You can use GitHub's "Edit" button to do it without leaving the browser.) [1]: http://forum.dlang.org/post/mailman.286.1285099532.858.digitalmars-d puremagic.com
Jul 05 2013
prev sibling parent "Michael" <pr m1xa.com> writes:
It's looks like he modify a source code from std.typecons 
(something like class AutoImplement?) and a new thing called 
"wrap" was created.

Like two fingers on the asphalt)))))
Jul 05 2013