Skip to content

Instantly share code, notes, and snippets.

@benjaminsnorris
Created May 6, 2016 21:51
Show Gist options
  • Save benjaminsnorris/289a079d64ade150f3224beb4894391a to your computer and use it in GitHub Desktop.
Save benjaminsnorris/289a079d64ade150f3224beb4894391a to your computer and use it in GitHub Desktop.
Unmarshaling Factory
// From Bart Whiteley
public protocol UnmarshalingFactory: Marshal.ValueType {
associatedtype ConvertibleType = Self
static func createFromObject(object: MarshaledObject) throws -> ConvertibleType
}
extension UnmarshalingFactory {
public static func value(object: Any) throws -> ConvertibleType {
guard let convertedObject = object as? MarshaledObject else {
throw Error.TypeMismatch(expected: MarshaledObject.self, actual: object.dynamicType)
}
return try Self.createFromObject(convertedObject)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment