Fully dynamic d by opDotExp overloading

Don nospam at nospam.com
Sat Apr 18 23:00:50 PDT 2009


Steven Schveighoffer wrote:
> On Sat, 18 Apr 2009 14:05:30 -0400, Andrei Alexandrescu 
> <SeeWebsiteForEmail at erdani.org> wrote:
> 
>> Steven Schveighoffer wrote:
>>> I gave this a lot of thought, and I think here is a possible solution:
>>>  the main reason I'm hesitant on this idea is because of code like this:
>>>  class X
>>> {
>>>   auto opDotExp(string fname, T...)(T args)
>>>   {
>>>      if(fname == "blah")
>>>        return foo(args);
>>>      else if(fname == "blither")
>>>        return bar(args);
>>>      // else, nothing happens
>>>   }
>>> }
>>>  Which leaves code open to lots of compiled code that doesn't do the 
>>> right thing (or throws some runtime exception).  What would be nice 
>>> is if the default behavior is what statically bound functions do, 
>>> that is, compile error, and only let the cases be handled which the 
>>> author expects to handle.
>>
>> class X
>> {
>>    auto opDotExp(string fname, T...)(T args)
>>    {
>>       static if(fname == "blah")
>>         return foo(args);
>>       else static if(fname == "blither")
>>         return bar(args);
>>       else static assert(0, "Dunno how to "~fname);
>>    }
>> }
> 
> Yeah, I get that it can be done manually.  What I'm suggesting is that 
> the compiler makes sure the static assert occurs if thbe result of 
> compiling the template instance results in an empty function.  I look at 
> it like adding methods to a class, you don't have to define which 
> methods are not valid, all methods are by default invalid, only the ones 
> you define are allowed.  With opDotExp, you are forced to define not 
> only which calls to it are valid, but which ones aren't.  I'm saying, 
> don't require defining which ones aren't, just like if you wanted to add 
> methods to a class.  I'm sure I'm not explaining this perfectly...
> 
> For example, the swizzle example that has been brought up many times, 
> you want to handle 256 possible method names, but there are an infinite 
> number of method names.  You don't care about defining what to do in all 
> situations, only in the situations you care about.  But without compiler 
> help, you have to.
> 
> -Steve

You'll get a "missing return statement", except in the case where it's 
void. So it's only that one case where it's a problem.



More information about the Digitalmars-d mailing list