[swift-evolution] Proposals: (1) Forbidding custom `==` for value types, (2) `dispatch` keyword, (3) `default`-result for methods with `Self`, and (4) Poor-Mans-Existentials

Johannes Neubauer neubauer at kingsware.de
Thu Jul 14 17:26:10 CDT 2016


Hi Daniel,

I will answer backwards, since it is easier for me this way.

> Am 14.07.2016 um 22:47 schrieb Daniel Resnick <danielzresnick at gmail.com>:
> 
> Furthermore, why should properties pointing to reference types always use `===`? What if the reference type conforms to `Equatable`?

I proposed to use `===`, since a value of a reference a value type is referencing is the reference itself. But point taken. So a solution could be to use `==` for reference types implementing `Equatable`. But wait for the punch-line below ;).

> Forbidding custom `==` for value types would break equality for all Swift collection types, since they use reference types behind the scenes for storing memory and copy on write semantics.

If the storage implements `Equatable`, this problem should be solved, right?

Anyway (punch-line following), although I don’t know how exactly the low-level Value-Witness-Table works, but if it works as I would expect, then there should be only exactly one entry per „equal“ storage, and all collection types with the same data point to the same reference (which is very memory efficient and table lookup using hashing should be constant time). If this is correct, a check for `===` suffices. AFAIK, the low-level swift implementation already checks for `===` on value types that are stored on the heap (see [this blog post][0]) and don’t bother calling `==` if `===` holds.

All the best
Johannes

[0]: https://www.raywenderlich.com/112029/reference-value-types-in-swift-part-2
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 496 bytes
Desc: Message signed with OpenPGP using GPGMail
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160715/1e06833b/attachment.sig>


More information about the swift-evolution mailing list