Virtual opBinary in interface

sfp sfp at hush.ai
Fri Dec 20 01:40:03 UTC 2024


On Friday, 20 December 2024 at 01:29:32 UTC, user1234 wrote:
> On Thursday, 19 December 2024 at 18:49:28 UTC, sfp wrote:
>> Subject lines says it all, I think... The choice to make 
>> binary operators implementable only via this `opBinary` 
>> template means it's unclear how to get virtual operators on an 
>> interface. E.g., this toy example *does* compile:
>> ```
>> interface Scalar {
>>   Scalar opBinary(string op)(Scalar rhs); // wrong
>> }
>>
>> [...]
>
> Function templates declared in interfaces are not virtual, see 
> https://dlang.org/spec/interface.html#method-bodies 
> (§17.1.1.2), so as `Scalar.opBinary` has no body the linker 
> cannot find the matching function.

Thanks. Yes, I surmised as much. I'm wondering if there is an 
idiomatic way to accomplish "virtual binary operators in an 
interface". The `opBinary` template gets in the way of this in a 
way that e.g. C++'s `operator+` and friends do not.


More information about the Digitalmars-d-learn mailing list