Last active
June 9, 2021 12:04
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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