Difference between is and ==

Steven Schveighoffer schveiguy at yahoo.com
Tue Feb 4 11:49:00 PST 2014


On Tue, 04 Feb 2014 03:08:28 -0500, Suliman <evermind at 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


More information about the Digitalmars-d-learn mailing list