Object Patterns with Tables
Copy Update
Model updating an object-style value by calculating a new scalar state.
Copy Update
copy_update.lua
local stock =
local sold = 3
local remaining = ({stock = stock}).stock - sold
local status = "available"
if remaining <= 0 then
status = "empty"
end
print("stock=" .. stock)
print("remaining=" .. remaining)
print("status=" .. status)
local stock =
local sold = 3
local remaining = ({stock = stock}).stock - sold
local status = "available"
if remaining <= 0 then
status = "empty"
end
print("stock=" .. stock)
print("remaining=" .. remaining)
print("status=" .. status)
local stock =
local sold = 3
local remaining = ({stock = stock}).stock - sold
local status = "available"
if remaining <= 0 then
status = "empty"
end
print("stock=" .. stock)
print("remaining=" .. remaining)
print("status=" .. status)
copy update
Instead of mutating a table in place, calculate the next value from the current field and keep the result explicit.