www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - opEquals when your type occurs on the right hand side of an equality

reply NonNull <non-null use.startmail.com> writes:
I am creating a specialized bit pattern (secretly represented as 
a uint) as a struct S, but want to avoid `alias this` to maintain 
encapsulation excepting where I overtly say. Specifically, I want 
to avoid making arithmetic and inequalities available for S.

I have written opEquals to compare an S to a uint.

How do I write code to compare a uint to an S?
Jul 31 2019
parent =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 07/31/2019 01:03 PM, NonNull wrote:
 I am creating a specialized bit pattern (secretly represented as a uint) 
 as a struct S, but want to avoid `alias this` to maintain encapsulation 
 excepting where I overtly say. Specifically, I want to avoid making 
 arithmetic and inequalities available for S.
 
 I have written opEquals to compare an S to a uint.
 
 How do I write code to compare a uint to an S?
 
I didn't know that it works both ways already: import std.stdio; struct S { bool opEquals(uint u) { writeln("called"); return true; } } void main() { S s; s == 7; 7 == s; } There are two "called"s printed... Ali
Jul 31 2019