www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Duck typing for structs

reply Ash Logan <ash.logior gmail.com> writes:
Hello all,

I was experimenting with Kenji Hara's interfaces.d, and I thought it
would be interesting to be able to adapt not just objects but struct
values to an interface. So I came up with a hacky solution, adapter.d,
and put it on Google Docs at

http://goo.gl/Y7Oa

in case anyone is interested. The unit tests have examples of usage with
structs and classes (including classes with methods added by mixin
templates, and structs with aliases to methods); there are several weird
bugs relating to overloaded methods, but more things work than I
expected to when I started. I don't know if this could turn into
something useful, or if it's just a curiosity, but I wanted to see what
the D community thought of it.

-- Ash
Oct 21 2010
parent reply bearophile <bearophileHUGS lycos.com> writes:
Ash Logan:

 but I wanted to see what the D community thought of it.
Just a question. Aren't lines like: if (is(typeof(candidate) == const)) Better as static ifs? Bye, bearophile
Oct 21 2010
parent Ash Logan <ash.logior gmail.com> writes:
On 10/21/2010 12:58 PM, bearophile wrote:
 Just a question. Aren't lines like:
 if (is(typeof(candidate) == const))
 Better as static ifs?

 Bye,
 bearophile
Probably they are; this was my first try at compile-time functions, and while writing it I kept crossing the line between what could and could not be evaluated at compile-time/as a condition to a static if, so I made most of the "ifs" non-static until I got it working. -- Ash
Oct 21 2010