Defining Function Types
Part of the Introduction To Luau section of Coddy's Lua journey — lesson 28 of 73.
Functions are values — so functions have types. Luau lets you name a function's signature with a type alias, then require variables to hold only functions matching that shape:
type MathOp = (number, number) -> number
local add: MathOp = function(a, b)
return a + b
end
local multiply: MathOp = function(a, b)
return a * b
endA function type lists the parameter types in parentheses, then an arrow ->, then the return type: (number, number) -> number is "takes two numbers, returns a number". Note the arrow is -> — this is type syntax, used only in annotations, never to define a function's body.
Notice add and multiply don't re-annotate their parameters: the variable's type is MathOp, so the checker already knows a and b are numbers and that the body must return one. One alias, many conforming functions — a blueprint your whole codebase can share.
Function types shine wherever functions travel: variables, table fields, and parameters (callbacks!) can all demand a precise signature instead of "some function, hopefully the right one".
Challenge
EasyCreate three function type aliases:
StringProcessor— takes onestring, returns astringNumberCalculator— takes twonumbers, returns anumberBooleanChecker— takes onestring, returns aboolean
Implement six functions conforming to them (store each in a local annotated with its alias):
toUpperCase(StringProcessor) — uppercases the input (usestring.upper)addPrefix(StringProcessor) — prepends"Processed: "divide(NumberCalculator) — divides the first number by the secondpower(NumberCalculator) — raises the first number to the power of the second (use^)isEmpty(BooleanChecker) —trueif the string has length 0startsWithA(BooleanChecker) —trueif the string starts with"A"(usestring.sub(s, 1, 1))
Call and print, each on its own line:
toUpperCase("hello world")addPrefix("data")divide(20, 4)power(3, 4)isEmpty("")startsWithA("Apple")
Try it yourself
-- Write code here
-- type StringProcessor = (string) -> string
-- type NumberCalculator = (number, number) -> number
-- type BooleanChecker = (string) -> boolean
-- implement: toUpperCase, addPrefix, divide, power, isEmpty, startsWithA
-- then print the six results
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Introduction To Luau
1Getting Started with Luau
What Is Luau?Why Use Luau?Your First Luau CodeType Checking & Error ModesRecap: Introduction to Luau4Working with Functions
Typing Params & Return ValuesTyping Anonymous FunctionsFunctions Returning NothingOptional ParametersDefault Parameter ValuesVariadic FunctionsDefining Function TypesRecap: Typed Functions2Core Types
Basic Types: num, str, boolThe 'any' Type: Escape HatchThe 'unknown' TypeNil & Optional TypesType Inference in ActionExplicit Type AnnotationsRecap: Core Types Practice5Aliases, Unions, Intersections
Type Aliases for PrimitivesUnion TypesWorking with Union TypesLiteral TypesIntersection TypesCombining Type AliasesRecap: Advanced Type Combos3Typed Tables: Arrays & Maps
Typed ArraysAdding and Reading ElementsWhat is a Map Type?Declaring and Accessing MapsIterating TablesMixed-Shape TablesMulti-dimensional Typed Arraystable.unpack and VarargsRecap: Arrays and Maps