Should we add `a * b` for vectors?

Timon Gehr timon.gehr at gmx.ch
Thu Oct 5 11:36:23 UTC 2017


On 05.10.2017 11:52, Walter Bright wrote:
> On 10/5/2017 2:13 AM, Timon Gehr wrote:
>> It's easy to explain why: In C++, operators are the _only_ functions 
>> that have UFCS.
>>
>> This is in stark contrast to D, where all functions _but_ operators 
>> have UFCS.
>>
>> The proposal was allow UFCS also for overloaded operators.
>>
>> Hence, this discussion is about UFCS. These are not really operator 
>> overloading issues.
> 
> Ok, but I'm not sure what the proposal was.

I forgot to answer to this.


> On 27 September 2017 at 17:41, Ilya Yaroshenko via Digitalmars-d <digitalmars-d at puremagic.com> wrote:
> I would prefer outer operator overloading be added to D instead of type wrappers. So a user can import a library for operations, rather then library of wrappers. --Ilya
> 

"outer operator overloading" is UFCS for operators. I.e.:

struct S{ int x; }
S opBinary(string op:"+")(S a,S b){ return S(a.x+b.x); }

void main(){
     auto s=S(3), t=S(4);
     import std.stdio;
     writeln(s+t); // S(7)
}

Starting from:

s+t

It rewritten to (as per the spec):
s.opBinary!"+"(t)

and then UFCS is applied (as per the spec):
opBinary!"+"(s,t)


I'm very much in favor of this. Also, those rewrites should be 
consistently applied for all types, even built-ins (the compiler 
implementation can be more complex, but the language rules would be 
simplified).
One immediate benefit would be that opCmp could be reliably used for all 
types that support comparison, for example 2.opCmp(3).
Another benefit would be that operators such as += can reassign class 
references, for example when a value type is implemented as a unique 
reference to immutable data.


More information about the Digitalmars-d mailing list