Fully dynamic d by opDotExp overloading
Christopher Wright
dhasenan at gmail.com
Fri Apr 17 15:22:25 PDT 2009
Nick Sabalausky wrote:
> "davidl" <davidl at nospam.org> wrote in message
> news:op.usje9ia3j5j59l at my-tomato...
>> The benefit is you don't need to write the call function,
>
> ...But you do have to write the opDotExp() function. How is that less work
> than just writing a dispatch function?
It's more work, but it lets you do other interesting things.
Prime example is one I mentioned earlier. Let's say we also get full
RTTI implemented soon (either in a library [I'm working on it] or in the
compiler). Then you can write a Variant type that you can call arbitrary
methods on. Neat, huh?
Let's say you integrate D with a scripting language where you can add
methods to an object at arbitrary times. Instead of writing:
scriptObj.invokeMethod("methodname", arguments);
You can instead write:
scriptObj.methodname(arguments);
This is a library type which has no business knowing about any methods
that you have defined on scriptObj. Moreover, the alternative is to
write this:
template ScriptMethod(string name)
{
enum ScriptMethod = "void " ~ name ~ "(...) { return
_vm.invokeMethod(_obj, \"" ~ name ~ \"" ~, _arguments, _argptr); }";
}
class ScriptObjWrapper
{
// repeat this a few dozen times for each method
mixin (ScriptMethod!("methodname"));
}
It's syntactic sugar, but it's really neat.
More information about the Digitalmars-d
mailing list