Array Protocol Extension

36 Views Asked by At

I am having a problem making an Array comply with a protocol.

I have defined a protocol which I use to tag types as able to store in my database:

protocol Storable {
}

now I want to make an String tagged as Storable

extension String: Storable {
}

An now I want an Array of Storable also tagged as Storable

extension Array: Storable where Element: Storable {
}

But, when I use this in this test example, an Array of type String is not seen as Storable

func x() -> Storable {
    var array: [Storable] = []
    return array // Error: Type 'any Storable' cannot conform to 'Storable'
}

I get an error: "Type 'any Storable' cannot conform to 'Storable'". But, I though I had defined an Array of values tagged with Storable to be Storable.

If I try the following:

extension Array: Storable where Element == Storable {
}

The test function now works OK:

func x() -> Storable {
    var array: [Storable] = []
    return array // No Error
}

But, not a concrete example using as String Array:

func y() -> Storable {
    var array: [String] = []
    return array // Error: 'y()' requires the types 'String' and 'any Storable' be equivalent
}

I get the error: 'y()' requires the types 'String' and 'any Storable' be equivalent

Any ideas? Thanks.

0

There are 0 best solutions below