Recap: Dispatch Reports
Lesson 29 of 35 in Coddy's Luau for Lua Developers course.
Challenge
MediumImplement two modules, short.luau and full.luau, each providing format(self,name,n). Short returns name:n; full returns name has n. Input has name, integer n, and style short or full. App selects the module and returns its format result.
Edit app.luau, short.luau, full.luau. The visible locked main.luau 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: Both formatter modules must implement their public format(self, name, n) method 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 short,full=require("./short"),require("./full")
assert(short:format("pen",3)=="pen:3","short module format")
assert(full:format("book",0)=="book has 0","full module format")
assert(short:format("x",-1)=="x:-1","signed values are formatted unchanged")
end
local app = require("./app")
local lines = {}
for line in io.lines() do lines[#lines + 1] = line end
print(app.run(lines))
All lessons in Luau for Lua Developers
4Functions and Closures
Functions and ReturnsDefaults and Options TablesMultiple Results and VarargsCallbacks and Captured StateRecap: Pricing Functions7Objects and Owned State
Tables as Instancesself and Colon CallsIndependent Instance TablesRecap: Account Objects5Tables as Collections
Lists as Dense TablesDictionaries and pairsAliasing and Shallow CopiesRecap: Count Inventory8Composition and Object Lookup
Composition and DelegationPrototype Lookup ChainsPolymorphism by ProtocolValue Equality MetamethodsRecap: Dispatch ReportsPractice on your own: Online Luau compiler