Can I ensure that two strings can share indexes?

Interestingly we've just reinvented identity operator.

infix operator ==== : ComparisonPrecedence
infix operator !=== : ComparisonPrecedence

func ==== <T>(lhs: T, rhs: T) -> Bool {
    var lhs = lhs
    var rhs = rhs
    return memcmp(&lhs, &rhs, MemoryLayout<T>.size) == 0
}

func !=== <T>(lhs: T, rhs: T) -> Bool {
    !(lhs ==== rhs)
}

It gives the same result as "===" for reference types, and gives the useful "are these bit for bit equivalent values" for value types.

This raises a question: perhaps the "===" operator could be extended to work with value types?

2 Likes