Recap: Enums, the Luau Way
Part of the Introduction To Luau section of Coddy's Lua journey. Lesson 60 of 80.
Challenge
MediumDescribe an order with an enum and a frozen table.
- Write
type OrderStatus = "pending" | "shipped" | "delivered"andtype Order = {id: number, status: OrderStatus}. - Write
describeOrder(order: Order): stringreturning`Order #{order.id} is {order.status}`. - Create
local Priority = table.freeze({Low = 1, High = 2}), then printdescribeOrder({id = 1002, status = "shipped"})andPriority.High.
Press Run Code: the output shows Order #1002 is shipped and 2.
Try it yourself
-- 1) type OrderStatus and type Order
-- 2) describeOrder(order: Order): string
-- 3) the frozen Priority table, then the two prints
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 ValuesDefaults for BooleansVariadic FunctionsDefining Function TypesRecap: Typed Functions2Core Types
Number, String and BooleanThe '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 Combos8Enums, the Luau Way
The Enum Pattern in LuauNumeric Enums with TablesString Enums as UnionsUsing Literal Union EnumsFreezing Constant TablesRecap: Enums, the Luau Way3Typed Tables: Arrays & Maps
Typed ArraysAdding and Reading ElementsWhat is a Map Type?Declaring and Accessing MapsIterating TablesMixed-Shape TablesReading a GridBuilding a Gridtable.unpackRecap: Arrays and MapsPractice on your own: Online Luau compiler