[swift-users] Workaround for generics not currently supporting conditional conformance to a protocol

Tim Vermeulen tvermeulen at me.com
Wed Nov 16 00:34:58 CST 2016


Not really, unfortunately. The standard library is littered with types because of exactly this (e.g. `MutableRangeReplaceableRandomAccessSlice`).

Conditional conformance can’t come fast enough!

> Hi All,
> 
> Does anyone have a good workaround for generics not currently supporting conditional conformance to a protocol. As stated in the Generics Manifesto something like this would be nice:
> 
> extension Array: Equatable where Element: Equatable {
> static func ==(lhs: Array, rhs: Array) ->Bool { ... }
> }
> 
> But I would currently write a wrapper, something like:
> 
> struct ArrayE<T: Equatable>{
> var elements: [T]
> }
> extension ArrayE: Equatable {
> static func ==(lhs: ArrayE, rhs: ArrayE) ->Bool { ...}
> }
> 
> This can get unwieldy when there are a lot of conditional protocol extensions required, i.e. wrappers round wrappers.
> 
> Is there a better way?
> 
> Thanks for any tips,
> 
> -- Howard._______________________________________________
> swift-users mailing list
> swift-users at swift.org
> https://lists.swift.org/mailman/listinfo/swift-users
> 
> 
> 


More information about the swift-users mailing list