www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Difference between is and ==

reply "Suliman" <evermind live.ru> writes:
What difference between
if ((x = stdin.readln().chomp) is "q")
and
if ((x = stdin.readln().chomp) == "q")

?
Feb 04 2014
next sibling parent reply "Martijn Pot" <martijnpot52 gmail.com> writes:
On Tuesday, 4 February 2014 at 08:08:30 UTC, Suliman wrote:
 What difference between
 if ((x = stdin.readln().chomp) is "q")
 and
 if ((x = stdin.readln().chomp) == "q")

 ?
My interpretation of tdpl p57: 'is' compares for alias equality for arrays and classes. Otherwise they are the same.
Feb 04 2014
parent reply "Suliman" <evermind live.ru> writes:
 My interpretation of tdpl p57:

 'is' compares for alias equality for arrays and classes.
 Otherwise they are the same.
So should next code have same behavior if I will use is instead of == import std.stdio; import std.string; void main() { getchar(); } void getchar() { string x; if ((x = stdin.readln().chomp) == "q") writeln("it's is q"); else writeln("Not q"); } In case I am using is, I have never get first if expression is true.
Feb 04 2014
parent "Martijn Pot" <martijnpot52 gmail.com> writes:
On Tuesday, 4 February 2014 at 08:25:18 UTC, Suliman wrote:
 My interpretation of tdpl p57:

 'is' compares for alias equality for arrays and classes.
 Otherwise they are the same.
So should next code have same behavior if I will use is instead of == import std.stdio; import std.string; void main() { getchar(); } void getchar() { string x; if ((x = stdin.readln().chomp) == "q") writeln("it's is q"); else writeln("Not q"); } In case I am using is, I have never get first if expression is true.
My guess is the following: string is an immutable(char)[]. As string is an array, 'is' checks for alias equality. x is not an alias for the (unnamed?) string literal "q".
Feb 04 2014
prev sibling next sibling parent "bearophile" <bearophileHUGS lycos.com> writes:
Suliman:

 What difference between
 if ((x = stdin.readln().chomp) is "q")
 and
 if ((x = stdin.readln().chomp) == "q")

 ?
"is" performs a raw comparison of just the values, and the value of a string is its ptr and length field. While "==" compares their contents. So you want to use "==" here because you are interested to see if x contains the char 'q', because while their lengths could be equal, their ptr is surely different. Bye, bearophile
Feb 04 2014
prev sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Tue, 04 Feb 2014 03:08:28 -0500, Suliman <evermind live.ru> wrote:

 What difference between
 if ((x = stdin.readln().chomp) is "q")
 and
 if ((x = stdin.readln().chomp) == "q")

 ?
The first compares the pointer of the arrays. The second compares the contents of the array. Both check length as well for equality. In other words, the first will always be false (the ROM literal "q" will never have the same address as some heap block), the second will be true if the input was the string "q". More generally, 'is' should be a bitwise comparison of the variables. '==' should check for logical equality, whatever that means for the variable types. -Steve
Feb 04 2014