Skip to content

Instantly share code, notes, and snippets.

@neelbakshi94
Last active June 9, 2021 12:04
actor BankAccount {
let id = UUID()
private var balance: Double = 0.0
func send(_ amount: Double, to destination: BankAccount) async throws {
guard amount >= 0 else {
throw Error.negativeAmountTransfer
}
if (amount > balance) {
throw Error.insufficientFunds
}
else {
self.balance -= amount
await destination.deposit(amount) // if we try to do destination.balance += amount, the compiler will throw an error cause the `balance` variable can only be accessed via `self` reference
}
}
func deposit(_ amount: Double) {
guard amount >= 0 else {
throw Error.negativeAmountTransfer
}
self.balance += amount
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment