opBinary : Static ifs or specialization?
Dominikus Dittes Scherkl
dominikus.scherkl at continental-corporation.com
Wed Jun 24 09:23:59 UTC 2020
On Wednesday, 24 June 2020 at 09:01:28 UTC, claptrap wrote:
> On Wednesday, 24 June 2020 at 00:53:58 UTC, H. S. Teoh wrote:
>> On Tue, Jun 23, 2020 at 11:53:36PM +0000, claptrap via
>>>
>>
>> If your implementations are based on built-in operators, you
>> could use mixins to unify the implementations into one, e.g.:
>
> Ah yeah thats useful, to expand a bit what about if the RHS is
> a different type?
>
>
> Point!T opBinary(string op, RHS)(RHS rhs)
> {
> static if (is(RHS == Point!float) || is(RHS ==
> Point!double))
> {
> // .. etc..
> }
> else static if (is(RHS == float) || is(RHS == double))
> {
> // etc...
> }
> else static if (is(RHS == float[2]) || is(RHS == double[2]))
> {
> // .. etc...
> }
> assert(0); // type not supported
> }
>
> should that be split up?
I would say, depends on how you can implement what you need in
the shortest way (with lest code-duplication). If one operator
works with several types on RHS, but others don't, split the
operators. If all operators work the same for same RHS type but
different for different RHS types, split the types.
More information about the Digitalmars-d-learn
mailing list