Automatic Interface Implementation
vladimir at thecybershadow.net
Fri Jul 5 16:17:42 PDT 2013
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
>> 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 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
More information about the Digitalmars-d