Virtual opBinary in interface

user1234 user1234 at 12.de
Fri Dec 20 01:29:32 UTC 2024


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.


More information about the Digitalmars-d-learn mailing list