Post

Replies

Boosts

Views

Activity

guarding on enum value
I am attepting to guard depending on the value of an enum...This is a contrived example so bear with me...public struct Invoice { public enum EmailStatus { case NeverSent case SentPaymentDueNotice(attempts: Int) case SentPaidNotice } public var name: String public var email: String public var total: Float public var paid: Bool public var emailStatus: EmailStatus }I would like to do something like the following...public func email(invoice: Invoice) throws { guard case .SentPaidNotice != invoice.emailStatus else { // ERROR: "Variable binding in a condition requires an initializer" throw EmailError.AlreadySentPaidNotice } print("Sending invoice to\(invoice.name) <\(invoice.email)>") }Unfortunately I can only seem to get this working with the compilerpublic func email(invoice: Invoice) throws { guard case .SentPaidNotice = invoice.emailStatus else { throw EmailError.AlreadySentPaidNotice } print("Sending invoice to \(invoice.name) <\(invoice.email)>") }Which is the inverse of what I want. I can only get this behavior using if instead of guard, like...public func email(invoice: Invoice) throws { if case .SentPaidNotice = invoice.emailStatus { throw EmailError.AlreadySentPaidNotice } print("Sending invoice to \(invoice.name) <\(invoice.email)>") }It seems the if-case guard-case is only a binding operation, it would be nice if you could perform logical operations on it, or am I just missing something?
11
0
6.2k
Jun ’15