Bug 346 – 'is' operator inconsistant with arrays

Status
RESOLVED
Resolution
INVALID
Severity
normal
Priority
P3
Component
dmd
Product
D
Version
D1 (retired)
Platform
x86
OS
All
Creation time
2006-09-12T23:42:00Z
Last change time
2014-02-15T13:21:42Z
Assigned to
bugzilla
Creator
ibisbasenji

Comments

Comment #0 by ibisbasenji — 2006-09-12T23:42:53Z
It has been my understanding that the 'is' operator, when working with types other than objects, is an alias for the '==' operator. (For template simplicity, as I recall.) However, the following code illustrates a case where it is different in behavior. (My apologies if this is not really a bug, but I don't know what else to call it.) -------------------------------------------------- import std .stdio ; void main () { static int[] foo = [1, 2, 3] , bar = [1, 2, 3] ; int[] def = foo; if (foo == bar) writefln(" foo == bar"); if (foo is bar) writefln(" foo is bar"); if (foo == def) writefln(" foo == def"); if (foo is def) writefln(" foo is def"); } -------------------------------------------------- Outputs: foo == bar foo == def foo is def
Comment #1 by bugzilla — 2006-09-12T23:48:25Z
"==" means the contents of the array are the same. "is" means the arrays occupy the same location in memory. The program is behaving as expected.
Comment #2 by ibisbasenji — 2006-09-13T00:02:30Z
Fair enough, and that does make sense. Although, having experimented a little further, I see that 'is' is using the .ptr of the arrays (logical). This means that any slice which begins at index 0 will also match true to an 'is'. Much to be considered when writing templates with arrays in mind.
Comment #3 by ibisbasenji — 2006-09-13T00:04:22Z
(In reply to comment #2) > Fair enough, and that does make sense. Although, having experimented a little > further, I see that 'is' is using the .ptr of the arrays (logical). This means > that any slice which begins at index 0 will also match true to an 'is'. Much > to be considered when writing templates with arrays in mind. > Gah, no, I retract that. It does check the other properties, just in case. This is good. :) I can work with that.