[swift-evolution] Synthesizing Equatable, Hashable, and Comparable for tuple types
Douglas Gregor
dgregor at apple.com
Fri Nov 24 17:47:43 CST 2017
> On Nov 22, 2017, at 2:59 PM, Mike Kluev <mike.kluev at gmail.com> wrote:
>
> on Date: Tue, 21 Nov 2017 22:54:21 -0800 Douglas Gregor <dgregor at apple.com <mailto:dgregor at apple.com>> wrote:
>
> > On Nov 21, 2017, at 10:48 PM, David Hart <david at hartbit.com <mailto:david at hartbit.com>> wrote:
> >
> > On 22 Nov 2017, at 07:41, Douglas Gregor via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org> <mailto:swift-evolution at swift.org <mailto:swift-evolution at swift.org>>> wrote:
> >
> >>
> >> I think it’s straightforward and less ugly to make structural types allow extensions and protocol conformances.
> >
> > Can somebody explain to me what is less ugly about that? I would have naturally thought that the language would be simpler as a whole if there only existed nominal types and all structural types were just sugar over them.
>
> See Thorsten’s response with, e.g.,
>
> Function<Double, InoutParam<String>, Param<Int>>
>
> which handles “inout” by adding wrappers around the parameter types (which one would have to cope with in any user of Function), but still doesn’t handle argument labels. To handle argument labels, we would need something like strings as generic arguments. We’d also need to handle calling conventions and anything else we invent for function types.
>
>
> can you outline how extensions and protocol conformances might look for structural types? to compare the ugliness of both approaches.
There are some examples at
https://github.com/apple/swift/blob/master/docs/GenericsManifesto.md#extensions-of-structural-types
e.g., making all tuples of Equatable elements Equatable (which also mixes in conditional conformances and variadic generics):
extension<...Elements : Equatable> (Elements...) : Equatable { // extending the tuple type "(Elements...)" to be Equatable
}
One could imagine adding a “curry” operation to function types:
extension<Param1, Param2, Result> (Param1, Param2) -> Result {
var curried: (Param1) -> (Param2) -> Result {
return { (arg1: Param1) in { (arg2: Param2) in self(arg1, arg2) } }
}
}
Or perhaps making metatypes Hashable so they can be used as keys into a Dictionary:
extension<T> T.Type: Hashable {
var hashValue: Int {
return ObjectIdentifier(self).hashValue
}
static func ==(lhs: T.Type, rhs: T.Type) -> Bool { /* standard library magic */ }
}
- Doug
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20171124/c718fa93/attachment.html>
More information about the swift-evolution
mailing list