opBinary : Static ifs or specialization?
H. S. Teoh
hsteoh at quickfur.ath.cx
Wed Jun 24 00:53:58 UTC 2020
On Tue, Jun 23, 2020 at 11:53:36PM +0000, claptrap via Digitalmars-d-learn wrote:
> So you have opBinary and half a dozen operators to implement. Do you
> use a separate method for each operator or do you have one method and
> a big static if else if to select code path?
[...]
If your implementations are based on built-in operators, you could use
mixins to unify the implementations into one, e.g.:
struct MyInt {
int i;
MyInt opBinary(string op)(MyInt arg) {
return MyInt(mixin("i "~op~" arg.i"));
}
}
which has far less boilerplate than separately implementing each
operator. This is one of the main reasons for the design of .opBinary.
T
--
Let's eat some disquits while we format the biskettes.
More information about the Digitalmars-d-learn
mailing list