Identity operators in Swift

Tao

If a is identical to c, b is identical to c, why a is not identical to b?

var a = [1, 2, 3]
var b = a
var c = a[0...2]
a === c                    // true
b === c                    // true
a === b                    // false

If a, b, c are constants:

let a = [1, 2, 3]
let b = a
let c = a[0...2]
a === c                    // true
b === c                    // true
a === b                    // true
shucao

As @onevcat said, it's might be a bug of Playground. And if you change a to objects of reference type, all the identity tests will be true.

class K {}
var a = [K(), K(), K()]
var b = a
var c = a[0...2]
a === c                    // true
b === c                    // true
a === b                    // true

it means that a, b & c share the same storage and elements.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do the PHP equality (== double equals) and identity (=== triple equals) comparison operators differ?

Swift - how to create custom operators to use in other modules?

Array of Arithmetic Operators in Swift

Several errors in Swift when using || or && logical operators

Swift operators and nil

CGAffineTransform.identity in Swift 2.3

Can you overload type-casting operators in Swift?

"Adjacent operators are in non-associative precedence group 'ComparisonPrecedence'" error in Swift

Getting Type identity of Swift type by name

What's the difference between &<< and << operators in Swift?

Python Identity operators with variables and datastructures

Swift custom operators with Unicode combining characters

Swift: Extending Combine operators

Type and Invocation of Swift's operators

Overloading Operators in Swift

Ternary Operators For Initialisation In Swift

How to assign "bigger than but smaller than" using Comparison Operators in Swift?

Unable for to get dev auth identity working in Swift

How to get Unauthenticated identity using Swift

Logical operators 'AND' / 'OR' in Swift while loop

Swift fluent-like api design with custom operators

Binary operators with Swift 3

How to get identity of sender in swift 3

Does Swift support automatic generation of operators?

Swift String array with arithmetic operators addition operation

Custom Range Operators - Swift 4.1

swift Logical Operators orderly

Swift short-circuiting with logical operators not working as expected

Behaviour of inout within Swift operators