This piece of code allows us to see the associated data, no problem:
do {
try foo()
} catch {
if case FaintingCouch.ClutchPearls(let msg) = error {
print(msg) // Prints "Test"
}
}
when we have two cases, i’m guessing that the ability of the type checker to know definitely what the structure of the FaintingCouch enum is, and therefore it’s ability to print it out automatically, is compromised by having more than 1 case.
maybe it should be able to figure out that the two cases share a common structure, but that seems like asking a lot of the compiler.
notice that conforming to CustomDebugStringConvertible with the following method achieves what I expect is the desired result:
public var debugDescription: String {
switch self {
case .ClutchPearls(let msg):
return "FaintingCouch(ClutchPearls(\"\(msg)\")"
case .GaspLoudly(let msg):
return "FaintingCouch(GaspLoudly(\"\(msg)\")"
}
}
There’s no good reason that the compiler cannot generate the full version with multiple cases when it seems to generate the debugDescription(). That’s the bug.
5 Comments
It’s not just with errors, it’s enums in general.
I think this was in previous betas too.
You can always use a switch recover the associated value
i don’t think this is an actual bug.
This piece of code allows us to see the associated data, no problem:
when we have two cases, i’m guessing that the ability of the type checker to know definitely what the structure of the FaintingCouch enum is, and therefore it’s ability to print it out automatically, is compromised by having more than 1 case.
maybe it should be able to figure out that the two cases share a common structure, but that seems like asking a lot of the compiler.
notice that conforming to CustomDebugStringConvertible with the following method achieves what I expect is the desired result:
There’s no good reason that the compiler cannot generate the full version with multiple cases when it seems to generate the debugDescription(). That’s the bug.
I was exploring how to get this to work the other day, and it appears I succeeded. The key is to add “let” before error:
enum Foo : ErrorType {
case First(String)
case Second(Int)
}
func flup(i: Int) throws -> Bool {
if i == 0 {
throw Foo.First(“Howdie”)
}
if i == 1 {
throw Foo.Second(2)
}
if i == 2 {
throw Foo.Second(4)
}
return true
}
print(“Howdie”)
do {
try flup(0)
} catch Foo.First(let error) {
print(“ERROR: \(error)”)
} catch {
print(“WTF: \(error)”)
}
do {
try flup(1)
} catch Foo.First(let error) {
print(“ERROR 1: \(error)”)
} catch Foo.Second(let error) {
print(“ERROR 2: \(error)”)
} catch {
print(“WTF: \(error)”)
}
do {
try flup(2)
} catch Foo.First(let error) {
print(“ERROR 1: \(error)”)
} catch Foo.Second(let error) {
print(“ERROR 2: \(error)”)
} catch {
print(“WTF: \(error)”)
}
Howdie
ERROR: Howdie
ERROR 2: 2
ERROR 2: 4