Object Patterns with Tables
Method Dispatch
Pick behavior from an action name the way a method dispatcher would.
Method Dispatch
method_dispatch.lua
local action =
local label = "unknown"
if action == "open" then
label = "opening"
end
if action == "close" then
label = "closing"
end
if action == "pause" then
label = "paused"
end
local message = ({action = action, label = label}).action .. ":" .. ({action = action, label = label}).label
print("action=" .. action)
print("message=" .. message)
local action =
local label = "unknown"
if action == "open" then
label = "opening"
end
if action == "close" then
label = "closing"
end
if action == "pause" then
label = "paused"
end
local message = ({action = action, label = label}).action .. ":" .. ({action = action, label = label}).label
print("action=" .. action)
print("message=" .. message)
local action =
local label = "unknown"
if action == "open" then
label = "opening"
end
if action == "close" then
label = "closing"
end
if action == "pause" then
label = "paused"
end
local message = ({action = action, label = label}).action .. ":" .. ({action = action, label = label}).label
print("action=" .. action)
print("message=" .. message)
method dispatch
Object methods often select behavior from an action name and the object's current fields.