Skip to content

Instantly share code, notes, and snippets.

@tarunon
Last active January 15, 2016 09:35
Show Gist options
  • Select an option

  • Save tarunon/ea51b3ccdcbca81b8d8d to your computer and use it in GitHub Desktop.

Select an option

Save tarunon/ea51b3ccdcbca81b8d8d to your computer and use it in GitHub Desktop.
workaround Wildcard error type (is typed)
enum Error1<E1: ErrorType> {
case ERROR1(E1)
init?(error: ErrorType) {
if let error = error as? E1 {
self = .ERROR1(error)
}
return nil
}
var error: ErrorType {
switch self {
case .ERROR1(let error):
return error
}
}
func expand<E2: ErrorType>() -> Error2<E1, E2> {
return Error2(error: self.error)!
}
}
enum Error2<E1: ErrorType, E2: ErrorType> {
case ERROR1(E1)
case ERROR2(E2)
init?(error: ErrorType) {
if let error = error as? E1 {
self = .ERROR1(error)
} else if let error = error as? E2 {
self = .ERROR2(error)
}
return nil
}
var error: ErrorType {
switch self {
case .ERROR1(let error):
return error
case .ERROR2(let error):
return error
}
}
func expand<E3: ErrorType>() -> Error3<E1, E2, E3> {
return Error3(error: self.error)!
}
}
enum Error3<E1: ErrorType, E2: ErrorType, E3: ErrorType> {
case ERROR1(E1)
case ERROR2(E2)
case ERROR3(E3)
init?(error: ErrorType) {
if let error = error as? E1 {
self = .ERROR1(error)
} else if let error = error as? E2 {
self = .ERROR2(error)
} else if let error = error as? E3 {
self = .ERROR3(error)
}
return nil
}
var error: ErrorType {
switch self {
case .ERROR1(let error):
return error
case .ERROR2(let error):
return error
case .ERROR3(let error):
return error
}
}
func expand<E4: ErrorType>() -> Error4<E1, E2, E3, E4> {
return Error4(error: self.error)!
}
}
enum Error4<E1: ErrorType, E2: ErrorType, E3: ErrorType, E4: ErrorType> {
case ERROR1(E1)
case ERROR2(E2)
case ERROR3(E3)
case ERROR4(E4)
init?(error: ErrorType) {
if let error = error as? E1 {
self = .ERROR1(error)
} else if let error = error as? E2 {
self = .ERROR2(error)
} else if let error = error as? E3 {
self = .ERROR3(error)
} else if let error = error as? E4 {
self = .ERROR4(error)
}
return nil
}
var error: ErrorType {
switch self {
case .ERROR1(let error):
return error
case .ERROR2(let error):
return error
case .ERROR3(let error):
return error
case .ERROR4(let error):
return error
}
}
// func expand<E5: ErrorType>() -> Error5<E1, E2, E3, E4, E5> {
// return Error5(error: self.error)!
// }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment