How to require operator overloading in interface

JS js.mdnq at gmail.com
Sat Jul 20 11:53:43 PDT 2013


On Saturday, 20 July 2013 at 18:34:30 UTC, Jesse Phillips wrote:
> On Saturday, 20 July 2013 at 16:46:52 UTC, JS wrote:
>> 	class MyClass {
>> 		auto opBinary(string op : "|" T : int)(T t) { }
>>                // opBinary is completely specialized and is no 
>> different than a regular function, it can be overridden 
>> directly in children without having to use a redirection. 
>> (note in your opBinaryImpl, T must be specified
>> 	}
>
> I understand there is a little boilerplate when wanting to have 
> overriding for the operator overloads, but I don't see much 
> gain from the proposal.
>
> Do you really want to force people to write
>
>     int opBinary(string op : "|" T : int)(T t)
>
> instead of
>
>     override int opOr(int t)
>
> when trying to override your function?
>

well, I see your point but it is less verbose AND if they want to 
allow the same op in there class, which they probably do, they 
have to override the long hand anyways and redirect. So all in 
all I think the long hand is better(more consistent with 
"previous practices").

> Though it might be interesting if an aliased function could be 
> overridden:
>
> class I {
>     final int opBinary(string op)(int t) { ... }
>     alias opAdd = opBinary!"+";
> }
>
> class MyClass : I{
>     override int opAdd(int v) { ... }
> }

I think this would be better. This way either choice could be 
used but there is no extra redirection that takes place(well, 
through the alias but that is symbolic).


More information about the Digitalmars-d-learn mailing list