34 lines
1.0 KiB
Mojo
34 lines
1.0 KiB
Mojo
# probe: a struct method that raises, caught by the caller
|
|
# status: the expected output is Programming+'s interpreter's prediction; corpus.py verify confirms or refutes it on godbolt
|
|
# expect-stdout: withdrew 30 left 120
|
|
# expect-stdout: refused 200 : insufficient funds
|
|
# expect-stdout: withdrew 120 left 0
|
|
# expect-stdout: ada 0
|
|
struct Account(Movable):
|
|
var owner: String
|
|
var balance: Int
|
|
|
|
def __init__(out self, owner: String, balance: Int):
|
|
self.owner = owner
|
|
self.balance = balance
|
|
|
|
def withdraw(mut self, amount: Int) raises:
|
|
if amount > self.balance:
|
|
raise Error("insufficient funds")
|
|
self.balance -= amount
|
|
|
|
def deposit(mut self, amount: Int):
|
|
self.balance += amount
|
|
|
|
|
|
def main():
|
|
var acct = Account("ada", 100)
|
|
acct.deposit(50)
|
|
for amount in [30, 200, 120]:
|
|
try:
|
|
acct.withdraw(amount)
|
|
print("withdrew", amount, "left", acct.balance)
|
|
except e:
|
|
print("refused", amount, ":", e)
|
|
print(acct.owner, acct.balance)
|