Menu
CoddyTech

Recap: Defining Table Shapes

Part of the Introduction To Luau section of Coddy's Lua journey. Lesson 46 of 80.

challenge icon

Challenge

Medium

Describe two computers, one with unknown RAM.

  1. Write type Computer = {id: number, brand: string, ramInGB: number?} and annotate workstation and laptop as Computer.
  2. Write local function specs(computer: Computer): string returning `{computer.brand} with {computer.ramInGB} GB of RAM` when RAM is known, otherwise `{computer.brand} with unknown RAM`.
  3. Print specs(workstation) and specs(laptop).

Press Run Code: the output shows Dell with 16 GB of RAM and MacBook with unknown RAM.

Try it yourself

-- 1) type Computer = {...}

local workstation = {id = 1001, brand = "Dell", ramInGB = 16}
local laptop = {id = 1002, brand = "MacBook"}

-- 2) specs(computer: Computer): string
-- 3) print specs for both
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Introduction To Luau

Practice on your own: Online Luau compiler