local Tiger = {}
Tiger.__index = Tiger
function Tiger.new(age)
return setmetatable({age = age}, Tiger)
end
-- Every branch has its own condition
local function describe(value)
local kind = type(value)
-- The type test goes first: it guards the rest
if kind == "number" and value > 1000000 then
return "Big number"
elseif kind == "string" and #value < 256 then
return "Short string"
elseif getmetatable(value) == Tiger
and value.age > 12 then
return "Old tiger"
else
return "Unknown type"
end
end
local old = describe(Tiger.new(15))
-- old = Old tiger
-- The condition fails: the next branch is tried
local young = describe(Tiger.new(5))
-- young = Unknown type
local big = describe(2000000)
-- big = Big number
local small = describe(42)
-- small = Unknown type
local text = describe("test")
-- text = Short string
print("old = " .. old)
print("young = " .. young)
print("big = " .. big)
print("small = " .. small)
print("text = " .. text)