The more interesting question

Alex Rønne Petersen alex at lycus.org
Wed May 16 10:52:07 PDT 2012


On 16-05-2012 19:36, Steven Schveighoffer wrote:
> On Wed, 16 May 2012 13:16:36 -0400, H. S. Teoh <hsteoh at quickfur.ath.cx>
> wrote:
>
>> On Wed, May 16, 2012 at 01:07:54PM -0400, Steven Schveighoffer wrote:
>> [...]
>>> For example:
>>>
>>> auto str = "abcabc";
>>> assert(str[0..3] == str[3..$]); // pass
>>> assert(str[0..3] is str[3..$]); // fail
>>>
>>> which is very counterintuitive.
>> [...]
>>
>> I don't find that counterintuitive at all. To me, 'is' concerns memory
>> identity: are the two things actually one and the same _in memory_? (In
>> this case, no, because they are different chunks of memory that just
>> happens to contain the same values.) Whereas '==' concerns logical
>> identity: do the two things represent the same logical entity? (In this
>> case, yes, these two arrays contain exactly the same elements.)
>>
>> I'd argue that 99% of the time, what you want is logical identity (i.e.,
>> ==), not memory identity.
>
> What's counter intuitive is if you use null as a 'special marker', then
> you use == in most cases, but that one case where you want to 'check for
> the special marker', in which case you *have* to use is.
>
> -Steve

I guess we can conclude that one should not use 'null' or 'is' for 
arrays unless absolutely necessary. '[]' and '==' should probably do for 
the majority of code.

-- 
Alex Rønne Petersen
alex at lycus.org
http://lycus.org


More information about the Digitalmars-d mailing list