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.7k
Feb ’23
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?
Replies
11
Boosts
0
Views
6.7k
Activity
Feb ’23
Apple Intelligence/ChatGPT in Virtual Machine
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
Replies
1
Boosts
0
Views
204
Activity
Jun ’25