I use Swift 3.1 and have some general structure:
struct Section<InfoT: Equatable, ItemsT: Equatable> {
let info: InfoT?
let items: [ItemsT]
}
And you want to expand the array using a custom method for elements of this generic type:
extension Array where Element == Section<Equatable, Equatable> {
// Just dummy example function:
func debugDummy() {
for section in self {
let info = section.info
print("section info: \(info).")
for item in section.items {
print("item: \(item).")
}
}
}
}
This gives me compilation errors:
error: using 'Equatable' as a concrete type conforming to protocol 'Equatable' is not supported
extension Array where Element == Section<Equatable, Equatable> {
^
error: GenericsListPlayground.playground:6:24: error: value of type 'Element' has no member 'info'
let info = section.info
^~~~~~~ ~~~~
error: GenericsListPlayground.playground:8:25: error: value of type 'Element' has no member 'items'
for item in section.items {
^~~~~~~ ~~~~~
How to declare such an extension? I tried several options for declaring this extension, for example:
extension Array where Element == Section (no arguments)
gives:
error: reference to generic type 'Section' requires arguments in <...>
etc ... none of them wants to compile.
source
share