Difference between is and ==

Martijn Pot martijnpot52 at gmail.com
Tue Feb 4 00:37:53 PST 2014


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".


More information about the Digitalmars-d-learn mailing list