www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - is vs ==

reply Brother Bill <brotherbill mail.com> writes:
For the modern D developer, which is the more preferred choice, 
to use 'is' or '=='?

Do the Style Guidelines or other authorities prefer one to the 
other for modern D coding?

Or is either equally satisfactory?
Aug 30
next sibling parent monkyyy <crazymonkyyy gmail.com> writes:
On Saturday, 30 August 2025 at 22:15:26 UTC, Brother Bill wrote:
 For the modern D developer, which is the more preferred choice, 
 to use 'is' or '=='?

 Do the Style Guidelines or other authorities prefer one to the 
 other for modern D coding?

 Or is either equally satisfactory?
different things `is` is mostly a special case like `is null`
Aug 30
prev sibling next sibling parent Steven Schveighoffer <schveiguy gmail.com> writes:
On Saturday, 30 August 2025 at 22:15:26 UTC, Brother Bill wrote:
 For the modern D developer, which is the more preferred choice, 
 to use 'is' or '=='?
`is` will do a direct bitwise comparison of the two sides, and cannot be hooked with an operator overload. This tests for *identity* (are the two sides the same instance). `==` will use a type-specific comparison controlled by either the lhs type or the rhs type. This tests for *logical equality*.
 Do the Style Guidelines or other authorities prefer one to the 
 other for modern D coding?

 Or is either equally satisfactory?
For value types that do not hook logical equality, `is` and `==` can be the same operation. There are some exceptions, for example floating-point numbers. `is` is generally preferred for pointer or reference comparisons. -Steve
Aug 30
prev sibling next sibling parent IchorDev <zxinsworld gmail.com> writes:
On Saturday, 30 August 2025 at 22:15:26 UTC, Brother Bill wrote:
 Do the Style Guidelines or other authorities prefer one to the 
 other for modern D coding?
No: https://dlang.org/dstyle.html
 For the modern D developer, which is the more preferred choice, 
 to use 'is' or '=='?
This question doesn't make much sense. It's like if you asked the same thing about delegates vs functions. `is` tells you if its operands are exact bit-for-bit matches, whereas `==` usually compares the contents of two things and may be overloaded using `opCmp` or `opEquals`. For clarity's sake, `is` should generally be used for pointer comparison.
Aug 31
prev sibling parent "H. S. Teoh" <hsteoh qfbox.info> writes:
On Sat, Aug 30, 2025 at 10:15:26PM +0000, Brother Bill via Digitalmars-d-learn
wrote:
 For the modern D developer, which is the more preferred choice, to use
 'is' or '=='?
[...] They have two different meanings. `is` is for determining identity (is this the same variable); whereas `==` is for comparing values (do these two variables have the same value). They are not interchangeable. T -- There are 10 kinds of people in the world: those who can count in binary, and those who can't.
Aug 31