Skip to content

Instantly share code, notes, and snippets.

@landonf
Last active August 29, 2015 14:02
Show Gist options
  • Save landonf/bc508690c248e4cb1728 to your computer and use it in GitHub Desktop.
Save landonf/bc508690c248e4cb1728 to your computer and use it in GitHub Desktop.
class TupleCodec<A, B> : Codec {
typealias CodecType = (A, B)
// What I'd really like to do here ...
typealias CodecA = Codec where CodecA.CodecType == A
typealias CodecB = Codec where CodecB.CodecType == B
let encA: CodecA
let encB: CodecB
// Or
// let encA: Codec<A>
// let encB: Codec<B>
// Or (and if I'm shooting for the moon, I really need covariant type constraints to accept both Codec<String> and Codec<Any>, etc...)
//
// let encA: Codec forSome { CodecType == A }
// let encB: Codec forSome { CodecType == B }
init (_ encA: CodecA, _ encB: CodecB) {
self.encA = encA
self.encB = encB
}
func encode(value: (A, B)) -> Result<CodingErr, UInt8[]> {
return .Err({CodingErr.InvalidInput(msg: "Unimplemented, (encA, encB) apply (A, B)")})
}
func decode(value: Slice<UInt8>) -> Result<CodingErr, ((A, B), Slice<UInt8>)> {
return .Err({CodingErr.InvalidInput(msg: "Unimplemented, (encA, encB) apply (A, B)")})
}
}
protocol Codec : Encoder, Decoder {
typealias CodecType
}
protocol Encoder {
typealias CodecType
func encode (value: CodecType) -> Result<CodingErr, UInt8[]>
}
protocol Decoder {
typealias CodecType
func decode (value: Slice<UInt8>) -> Result<CodingErr, (CodecType, Slice<UInt8>)>
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment