wrap/unwrap vs Go-style duck typing

Jesse Phillips Jesse.K.Phillips+D at gmail.com
Sat Oct 26 16:03:52 PDT 2013


On Friday, 25 October 2013 at 05:45:53 UTC, Andrei Alexandrescu 
wrote:
> Hello,
>
> I was curious how our fledgling wrap and unwrap routines 
> compare with Go's duck typing - similarities, distinctions, 
> performance. Please share any thoughts, thanks!
>
> Andrei

This is what I've come up with.

It doesn't look like it works with structures? Go doesn't have 
classes so structures are going to provide the most "Go" like 
feel.

I expect it doesn't work with pseudo members. Go adds methods to 
a struct similar to C++ friend methods or the UFCS provided by D. 
Thus, it would feel more natural to a Go programmer to add 
"friend" functions and expect them to be usable by the 
typecons.wrap(). And on a D related note:

It would be nice to support UFCS during the wrapping, this comes 
back to the example of arrays. An array is only a range because 
std.range was imported, if a function checks to see 
if(isInputRange!R) then arrays would fail if that same module 
didn't import std.range. If wrap worked with the current scoped 
"friend" functions, then the caller could easily just 
arr.wrap!InputRange and move on. This just seems like the main D 
use-case for this type of feature.

I'm also not sure why this code is giving me linker errors. I've 
gotten the examples to work, but trying to mimic a Go blog isn't 
working:
http://blog.jessta.id.au/2011/06/golang-interfaces.html

     interface Writer { void Write(string); }

     void SomeFunction(Writer w){
         w.Write("pizza");
     }

     class Human {
         void Write(string s) {
             import std.stdio;
             writeln(s);
         }
     }
     void main() {
         Human h = new Human();;

         import std.typecons;
         SomeFunction(h.wrap!Writer);
     }

undefined reference to 
`_D3std8typecons24__T4wrapTC5write6WriterZ23__T4wrapTC5write5HumanZ4Impl308__T8mixinAllVAyaa143_6f766572726964652052657475726e5479706521...


More information about the Digitalmars-d mailing list