opDispatch and operator overloads

Timon Gehr timon.gehr at gmx.ch
Tue May 21 13:16:08 PDT 2013


On 05/21/2013 09:53 PM, Steven Schveighoffer wrote:
> On Tue, 21 May 2013 15:36:31 -0400, Timon Gehr <timon.gehr at gmx.ch> wrote:
>
>> On 05/21/2013 05:31 PM, Steven Schveighoffer wrote:
>>> ...
>>>
>>
>> This handles your case using opDispatch:
>>
>> import std.stdio,std.conv,std.algorithm,std.array;
>>
>> string commaSep(T...)(T args){
>>      string r="";
>>      foreach(a;args) r~=a.to!string~",";
>>      return r[0..$-!!$];
>> }
>>
>> struct S{
>>      template opDispatch(string name){
>>          template opDispatch(T...){
>>              auto opDispatch(S...)(S args){
>>  writeln(this,".",name,"!"~T.stringof~"(",commaSep(args),")");
>>              }
>>          }
>>      }
>> }
>>
>> void main(){
>>      S s;
>>      s.opBinary!"+"(2); // ok
>>      // s.foo(); // error. should IMO be fixed.
>>      s.foo!()(); // ok
>>      s.bar!([1,2,3],int)("123"); // ok
>> }
>>
>>
>>> what is the problem with just defining opBinary to catch missing
>>> operator overloads?
>>>
>>
>> It is more boilerplate.
>> https://github.com/D-Programming-Language/phobos/blob/bcf7dd9bd268956754bf1a034728bef29619e858/std/typecons.d#L2654
>>
>
> This pretty much makes my point.  I have no idea what your 3-level
> opDispatch does.

1st level: member name
2nd level: explicit template arguments
3rd level: ifti-deduced arguments.


> The proxy type, while verbose, I can understand.
>

Fair point.

> Yes, lots of boilerplate.  If anything, though, that's an argument to
> fix how operator overloading works.
>
> At the end of the day, it's going to be less boilerplate for the
> std.typecons.Proxy that one has to mixin.  User code will look the same.
>...

For user code there is a difference eg. in how inflated the 
__traits(allMembers) result will be.





More information about the Digitalmars-d mailing list