generics - Swift compiler crashes on switch statement, need workaround -
i've got code looks this, although i'm not sure distilled version reproduces compiler crash:
enum response<t> { case success(t) case failure(string) } struct responsedata { let somedata = "some data" } func evaluate() { let response = response.success(responsedata()) switch response { case let .success(data): println("got response \(data)") case let .failure(reason): println("got failure: \(reason)") default: () } } the xcode editor doesn't detect problems, when build, compiler crashes error:
command /applications/xcode.app/contents/developer/toolchains/xcodedefault.xctoolchain/usr/bin/swiftc failed exit code 1 when comment out .success case, can build fine. i'm guessing swift compiler doesn't generics associated values enum. however, pattern extremely useful me, , makes code more readable. idea how can work around this? far can tell, there's no way access associated values except switch statement.
i should mention have found the question here, have not managed make use of solutions presented.
edit following throws seg-fault:
enum response<t> { case success(t) case failure(string) } struct responsedata { let somedata = "some data" } func evaluate() { let response = response.success(responsedata()) } unimplemented ir generation feature non-fixed multi-payload enum layout enum response {
looks can use objects inherit nsobject
like this
enum result<t: nsobject>: printable { case success(t) case error(nserror) var description: string { switch self{ case .success(let obj): return obj.description case .error(let error): return error.description } } and then
let r = result.success("xcode") r.description upd: can this
@objc protocol resultobjectprotocol { var description: string { } } extension nsdictionary: resultobjectprotocol {} enum result<t: resultobjectprotocol>: printable { case success(t) case failure(nserror) var description: string { switch self{ case .success(let obj): return obj.description case .failure(let error): return error.description } } } let r = result.success(["d" : 132]).description note: var description - example
Comments
Post a Comment