But I don't want swift force using default equal comparator before my implementation when I knew it was redundant that everything checking twice.
I have wrote lots of structure type with cache object for lazy evaluation. All of them dose not need to compare the cache object. Johannes Neubauer <[email protected]> 於 2016年7月18日星期一 寫道: > > > Am 18.07.2016 um 06:47 schrieb Susan Cheng <[email protected] > <javascript:;>>: > > > > so, you want to propose default == operator but not forbidding all > peoples to custom == operator? > > Why don't just adding the following function to std library? > > > > public func == <T : Equatable>(lhs: T, rhs: T) -> Bool { > > var lhs = lhs > > var rhs = rhs > > return memcmp(&lhs, &rhs, sizeof(T.self)) == 0 > > } > > This does not work, because method parameters are statically dispatched. > This function will never be executed for any type, that has a custom > equality implementation. So this would not enforce this check upfront. You > would need to copy this code to every custom implementation (which can be > forgotten). Or you have to implement it like this > > ```swift > public func isSame(lhs: Any, rhs: Any) -> Bool { > // like above in your code > } > > public func ==(lhs: MyType, rhs: MyType) -> Bool { > if isSame(lhs, rhs: rhs) { > return true > } > // do custom behavior > } > ``` > > > Thread safety can't fixed by std library. Value type only can atomic > compared when additional mutex provided. > > Value types are either on the stack or they are **copied** to the heap > (for protocol types with large value types). So, you don’t have any thread > safety issues (as they are copied before they are changed in the new > thread) as long as you don’t do (or check) anything on a property of a > reference type, because the latter has shared **state**. >
_______________________________________________ swift-evolution mailing list [email protected] https://lists.swift.org/mailman/listinfo/swift-evolution
