dynamic classes and duck typing

Max Samukha spambox at d-coding.com
Tue Dec 1 02:56:03 PST 2009


On Mon, 30 Nov 2009 22:33:40 -0800, Walter Bright
<newshound1 at digitalmars.com> wrote:

>
>I agree. While the compiler currently doesn't check for mixing up 
>properties and methods, I intend to make it do so. I can't see any 
>justification for allowing it.

Bill rightfully mentioned that it would be impossible to dynamically
dispatch to both properties and methods even if those properties and
methods don't have conflicting names. And that may really be an
unfortunate limitation. For example, it would be problematic to
implement a generic wrapper for IDispatch:

class ComDispatcher
{
   this(IUnknown iUnk)
   {
      // query IDispatch and possibly create a member-names-to-id map,
etc.
   }

   Variant opDispatch(string method, T...)(T args)
   {
      // call method (using DISPATCH_METHOD)
   }

   @property
   void opDispatch(string property, T)(T arg)
   {
      // set property (using DISPATCH_PROPERTYPUT)
   }

   @property
   Variant opDispatch(string property)()
   {
      // get property (using DISPATCH_PROPERTYGET)
   }  
}

auto c = new ComDispatcher(iUnk);
c.foo(1); // call method
c.bar = 1; // set property
int a = c.baz; // get property
int b = c.qux(); // call method 



More information about the Digitalmars-d mailing list