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?
Selecting any option will automatically load the page
Post
Replies
Boosts
Views
Activity
Can the Xcode 26 code assist feature be used in a macOS 26 virtual machine? I am not seeing a way to enable it...
Also asking on https://github.com/insidegui/VirtualBuddy/discussions/524