Structural APIs to Delegation
Lesson 20 of 29 in Coddy's Lua for Luau Developers course.
A Luau structural type can require an object to provide a method without naming a class hierarchy. Lua 5.1 can keep the same collaboration by calling the supplied object through its public method. Remove the type alias, retain the object reference, and delegate instead of rebuilding its presumed internals. A caller may supply a custom implementation with no factor field at all. This matters in the task: the model must use the policy it receives, while the ordinary policy module provides one multiplication-based implementation. A small runtime check can validate a boundary, but it cannot replace the actual delegation behavior.
Luau
type Policy = {apply: (any, number) -> number}
local function total(policy: Policy, n: number): number return policy:apply(n) end
local policy = {apply=function(self, n) return n+7 end}
print(total(policy, 5))Lua 5.1
local function total(policy, n) return policy:apply(n) end
local policy = {apply=function(self, n) return n+7 end}
print(total(policy, 5))Porting trap
Hardcoding the standard policy formula in the model defeats the public API. A different object with the same method must remain usable.
Challenge
EasyImplement a pricing policy in policy.lua with apply(n) multiplying n by a stored factor. Model stores the policy and exposes total(n). Input is factor and quantity. Return the delegated total.
Edit app.lua, model.lua, policy.lua. The visible locked main.lua reads stdin into a sequence of lines, calls app.run(lines), and prints its returned value with one final newline. Do not print diagnostics or change the module names. Empty input gives an empty sequence. All numeric inputs are small enough to avoid overflow.
Public API checks: Policy.new(factor) provides apply(n). Model.new(policy) must delegate total(n) to the supplied policy, including a custom object implementing apply, and retain that collaborator independently. The locked driver checks this behavior before calling app.run. These checks print nothing when successful.
Try it yourself
-- Public API checks
do
local Model=require("model")
local Policy=require("policy")
local p=Policy.new(3)
assert(p:apply(4)==12 and p:apply(0)==0,"policy multiplies by its factor")
local a=Model.new(p)
assert(a:total(2)==6,"model must use its pricing policy")
local calls=0
local custom={apply=function(self,n) calls=calls+1;return n+7 end}
local b=Model.new(custom)
assert(b:total(5)==12 and calls==1,"model must delegate to the supplied collaborator")
assert(a:total(1)==3,"models retain their own policy")
end
local app = require("app")
local lines = {}
for line in io.lines() do lines[#lines + 1] = line end
print(app.run(lines))
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Lua for Luau Developers
1Syntax and Runtime
Run Lua ModulesInterpolation to TextFloor Division and NumbersBytes and UTF-8 LibrariesFalse Values and DefaultsConditional Expressions4Objects and Contracts
Types and Runtime ObjectsTyped self and Colon CallsIndependent Nested TablesStructural APIs to DelegationMetatable Library DifferencesEquality and Frozen ValuesRecap: Account ObjectsPractice on your own: Online Lua compiler