Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
September 13, 2006 [Issue 346] New: 'is' operator inconsistant with arrays | ||||
---|---|---|---|---|
| ||||
http://d.puremagic.com/issues/show_bug.cgi?id=346 Summary: 'is' operator inconsistant with arrays Product: D Version: 0.166 Platform: PC OS/Version: All Status: NEW Severity: normal Priority: P3 Component: DMD AssignedTo: bugzilla@digitalmars.com ReportedBy: ibisbasenji@gmail.com 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 -- |
September 13, 2006 [Issue 346] 'is' operator inconsistant with arrays | ||||
---|---|---|---|---|
| ||||
Posted in reply to d-bugmail | http://d.puremagic.com/issues/show_bug.cgi?id=346 bugzilla@digitalmars.com changed: What |Removed |Added ---------------------------------------------------------------------------- Status|NEW |RESOLVED Resolution| |INVALID ------- Comment #1 from bugzilla@digitalmars.com 2006-09-12 23:48 ------- "==" 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. -- |
September 13, 2006 Re: [Issue 346] New: 'is' operator inconsistant with arrays | ||||
---|---|---|---|---|
| ||||
Posted in reply to d-bugmail | <d-bugmail@puremagic.com> wrote in message news:bug-346-3@http.d.puremagic.com/issues/... > http://d.puremagic.com/issues/show_bug.cgi?id=346 > 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.) I'd say that this is proper, but just a fuzzy area. == compares the values of the arrays, and 'is' sees if the array references refer to the same data. The same way as class instances: == compares the values (using opEquals), and 'is' sees if they point to the same instance. |
September 13, 2006 [Issue 346] 'is' operator inconsistant with arrays | ||||
---|---|---|---|---|
| ||||
Posted in reply to d-bugmail | http://d.puremagic.com/issues/show_bug.cgi?id=346 ------- Comment #2 from ibisbasenji@gmail.com 2006-09-13 00:02 ------- 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. -- |
September 13, 2006 [Issue 346] 'is' operator inconsistant with arrays | ||||
---|---|---|---|---|
| ||||
Posted in reply to d-bugmail | http://d.puremagic.com/issues/show_bug.cgi?id=346 ------- Comment #3 from ibisbasenji@gmail.com 2006-09-13 00:04 ------- (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. -- |
Copyright © 1999-2021 by the D Language Foundation