Pow Expressions - not worth the juice?
jmh530
john.michael.hall at gmail.com
Tue Feb 3 15:51:05 UTC 2026
On Tuesday, 3 February 2026 at 15:23:09 UTC, user1234 wrote:
> [snip]
>> ```
>> struct Foo
>> double x;
>> bool opBinary(string op)(double y)
>> if (op == "%divisible%")
>> {
>> return x % y == 0 ? true : false;
>> }
>> }
>> ```
>> where the basically the `"op"` can be anything so long as it
>> is delimited in a form like "%op%"
>>
>> It would be even better if you could do something like (to my
>> knowledge `opBinary` would need to be a member function).
>>
>> ```
>> bool opBinary(string op)(double x, double y)
>> if (op == "%divisible%")
>> {
>> return x % y == 0 ? true : false;
>> }
>> ```
>>
>> [1] https://www.datamentor.io/r-programming/infix-operator
>
> So you just avoid the call-expression syntax so that you can
> put spaces between the operands and the operator ? I'm not sure
> this solves any problem. Maybe you can provide enlightning
> examples ? The one from your link is not convincing.
I was sketching out what a D version of the example from that
link, not that I would want to do that in D. We have UFCS.
The idea is that you can put anything in within the "op" of
"%op%", so it's on the user to decide how they want to make use
of it (or not). R uses "%*%" for matrix multiplication. That's
more ergonomic than `A.matmul(B)`.
More information about the Digitalmars-d
mailing list