[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
Mon Jul 18 07:01:03 CDT 2016


> Am 18.07.2016 um 13:52 schrieb Johannes Neubauer via swift-evolution <swift-evolution at swift.org>:
> 
>> 
>> Am 18.07.2016 um 13:05 schrieb L. Mihalkovic <laurent.mihalkovic at gmail.com>:
>> 
>> IMHO implementing your proposal would close the door on some of the things you do when building in-memory dbs (T == U -> TRUE for T not related to U), which if swift remains for small apps is not a terrible loss, but may be more of an issue for one day doing big-data with it.
> 
> You talk about reference types now, right? I proposed a `default` keyword, which (in a pattern matching fashion) would catch all calls to T == U for which no implementation exists (so this is exactly when T != U). You could of course change for a given type hierarchy the `default` result to `true` if appropriate.

This formulation can be misleading: I mean `a == b` where `a: T` and `b: U` and `T != U`. Due to dynamic dispatch even: `a.dynamicType == T && b.dynamicType == U && T != U`.

> 
> If you recall how virtual functions work. There is a V-table, which contains an entry for each overridden function and the correct one is looked up and executed with an implicit _self parameter for the instance on which the method has been executed.
> 
> You could write something like this:
> 
> ```swift
> // you could equally write: `func ==(lhs: dispatch A, rhs: dispatch A) -> Bool = false`
> dispatch func ==(lhs: A, rhs: A) -> Bool = false {
>  lhs === rhs
> }
> 
> // the default value `= false` is inferred...
> dispatch func ==(lhs: Aa, rhs: Aa) -> Bool {
>  lhs.a == rhs.a
> }
> ```
> 
> You could change the default value of course. For comparisons (`Comparable`) you could add a closure to calculate the default value (e.g. more specialized instances are greater than less specialized so Point3D is always greater than Point2D, in order to get total ordering).
> 
> The implementation would create a dispatch-table for this function. The key would be a tuple `(MetaType, MetaType)`. And the value would be the corresponding function. If the lookup `(lhs.dynamicType, rhs.dynamicType)` does not find a table entry (e.g. because it is `lhs.dynamicType != rhs.dynamicType`), then a auto-closure returning the default value (or if it is a closure, the closure itself) is returned and executed.
> 
> Cool?

All the best
Johannes



More information about the swift-evolution mailing list