Imports to require
Lesson 18 of 35 in Coddy's Luau for Lua Developers course.
Lua 5.1 require commonly uses package.searchers or package.loaders and module names. This Luau CLI uses relative file requests; do not port package configuration or expect unrestricted file IO. Coddy adds stdin APIs separately.
Lua 5.1 comparison
local first=require("math")
local again=require("math")
print(first == again)Luau behavior
The CLI loads local modules with relative paths such as require("./policy"). A module returns its public value. Requiring the same resolved module reuses its result. Built-in libraries such as math are already globals, not CLI files to load with require("math"). Module resolution is a host capability.
local first=require("./sample")
local again=require("./sample")
print(first == again)Expected Luau output:
trueThe sibling module sample.luau contains:
return {value=7}Challenge
EasyInput is integer price and nonnegative quantity. Implement subtotal in pricing.luau and call it from app.luau. Return the product. Do not print during require.
Edit app.luau, pricing.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: pricing.subtotal(price, quantity) must work independently of app.run, including zero quantity and signed price. The locked driver checks this behavior before calling app.run. These checks print nothing when successful.
Try it yourself
-- Public API checks
do
local pricing=require("./pricing")
assert(pricing.subtotal(4,3)==12 and pricing.subtotal(4,0)==0 and pricing.subtotal(-2,3)==-6,"pricing.subtotal must implement the public product API")
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 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 ObjectsPractice on your own: Online Luau compiler