Thread overview
How to do operator overloading for <, >, <=, >=, !=, and == between struct and int?
Apr 21, 2019
Ferhat Kurtulmuş
Apr 21, 2019
Adam D. Ruppe
Apr 21, 2019
Ferhat Kurtulmuş
April 21, 2019
I am writing an opencv binding and need something like: Mat m = another_mat > 5;
Docs does not cover this sitiuation: https://dlang.org/spec/operatoroverloading.html#compare
opBinary does not support those operators, and Section "Overloading <, <=, >, and >=" describes overloaded operators returning only int values.

My struct is defined here: https://github.com/aferust/opencvd/blob/master/source/opencvd/cvcore.d
April 21, 2019
On Sunday, 21 April 2019 at 18:07:08 UTC, Ferhat Kurtulmuş wrote:
> I am writing an opencv binding and need something like: Mat m = another_mat > 5;

D does not support that. The comparison operators are always just true or false (as determined by the int opCmp or the bool opEquals returns), they do not return other types.

You will probably have to use a named method instead. You could kinda fake it with a string template arg:

Mat m = another_map.cmp!">"(5);

But I'd probably just do it

Mat m = another_map.is_greater_than(5);

which imo is more readable. but the implementations are the same - in both cases, your custom function.
April 21, 2019
On Sunday, 21 April 2019 at 18:17:00 UTC, Adam D. Ruppe wrote:
> On Sunday, 21 April 2019 at 18:07:08 UTC, Ferhat Kurtulmuş wrote:
>> I am writing an opencv binding and need something like: Mat m = another_mat > 5;
>
> D does not support that. The comparison operators are always just true or false (as determined by the int opCmp or the bool opEquals returns), they do not return other types.
>
> You will probably have to use a named method instead. You could kinda fake it with a string template arg:
>
> Mat m = another_map.cmp!">"(5);
>
> But I'd probably just do it
>
> Mat m = another_map.is_greater_than(5);
>
> which imo is more readable. but the implementations are the same - in both cases, your custom function.

I am a little disappointed :( I will go for named method instead. Thank you for a fast response.