ludic/examples/modules/port_vars.ludic

24 lines
758 B
Text

# port_vars.ludic — L3: a port member that takes nothing can be bound to a variable, `money: g_money`
# - the compiler writes the getter (bind_Purse_money) in the bind's own file - and a member that takes
# something is still bound to a function.
#
# Running it prints: 40 45 12
program PortVars {
port Purse {
money: fn() -> int
price: fn(int) -> int
}
state PortvarsState {
g_money: int = 40
g_rate: int = 3
}
function priced(portvars_st: PortvarsState, n: int) -> int {
return n * portvars_st.g_rate
}
bind Purse { money: PortvarsState.g_money, price: fn priced }
entry (portvars_st: mut PortvarsState) {
let a = Purse.money()
portvars_st.g_money += 5
print(`{a} {Purse.money()} {Purse.price(4)}`)
}
}