Extend CollectionType intead of extending Array

Extending Array was great, but it'd be nicer if we could extend _any_
collection type that contains Decodable objects. This `decode` function
will still always return an Array, for the same reason that the stdlib's
`map`, `flatMap`, etc return Arrays, but if we ever get Real Actual
Higher Kinded Types, we can fix this.
This commit is contained in:
Gordon Fontenot and Giles Van Gruisen
2015-09-25 13:51:09 -04:00
committed by Tony DiPasquale
parent ec1ad342b3
commit c28ee37f73

View File

@@ -60,10 +60,10 @@ public extension Optional where Wrapped: Decodable, Wrapped == Wrapped.DecodedTy
}
}
public extension Array where Element: Decodable, Element == Element.DecodedType {
static func decode(j: JSON) -> Decoded<[Element]> {
public extension CollectionType where Generator.Element: Decodable, Generator.Element == Generator.Element.DecodedType {
static func decode(j: JSON) -> Decoded<[Generator.Element]> {
switch j {
case let .Array(a): return sequence(a.map(Element.decode))
case let .Array(a): return sequence(a.map(Generator.Element.decode))
default: return .typeMismatch("Array", actual: j)
}
}