Recap: Enums, the Luau Way
Part of the Introduction To Luau section of Coddy's Lua journey — lesson 56 of 73.
Challenge
MediumCreate a literal union type OrderStatus with members "pending", "shipped", "delivered" and "cancelled".
Create a shape alias Order with:
idof typenumbercustomerNameof typestringstatusof typeOrderStatus
Create three orders:
order1— id1001,"Alice Johnson", status"pending"order2— id1002,"Bob Smith", status"shipped"order3— id1003,"Carol Davis", status"delivered"
Create a function displayOrderInfo that takes an Order and prints Order #[id] for [customerName] is [status].
Create a frozen constant table Priority with Low = 1, Normal = 2, High = 3.
Then, in order:
- call
displayOrderInfofor each of the three orders - print the status of
order2 - print
Priority.High - print whether
Priorityis frozen
Try it yourself
-- Write code here
-- 1) define type OrderStatus (literal union) and type Order (shape)
-- 2) create order1, order2, order3
-- 3) write displayOrderInfo
-- 4) create the frozen Priority table
-- 5) print the six requested lines
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 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 TablesMulti-dimensional Typed Arraystable.unpack and VarargsRecap: Arrays and Maps