www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to do operator overloading for <, >, <=, >=, !=, and == between

reply Ferhat =?UTF-8?B?S3VydHVsbXXFnw==?= <aferust gmail.com> writes:
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
Apr 21 2019
parent reply Adam D. Ruppe <destructionator gmail.com> writes:
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.
Apr 21 2019
parent Ferhat =?UTF-8?B?S3VydHVsbXXFnw==?= <aferust gmail.com> writes:
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.
Apr 21 2019