Menu
Coddy logo textTech

Recap: Defining Table Shapes

Part of the Introduction To Luau section of Coddy's Lua journey — lesson 44 of 73.

challenge icon

Challenge

Medium

Create a type alias named Computer with:

  • id of type number
  • brand of type string
  • ramInGB of type number? (optional)
  • boot — a method (self: Computer) -> ()

Create two computers (attach boot with the colon definition syntax):

  • workstation — id 1001, brand "Dell", ramInGB 16; its boot prints [brand] is booting up...
  • laptop — id 1002, brand "MacBook", no ramInGB; its boot prints [brand] is starting...

Create a function specs that takes a Computer and returns a string: [brand] with [ramInGB] GB of RAM when RAM is known, otherwise [brand] with unknown RAM.

Then, in order:

  1. call workstation:boot()
  2. call laptop:boot()
  3. print specs(workstation)
  4. print specs(laptop)
  5. print the id of workstation

Try it yourself

-- Write code here
-- 1) define type Computer (optional ramInGB, boot method)
-- 2) create workstation and laptop, attach boot with colon syntax
-- 3) write specs with a nil check on ramInGB
-- 4) boot both, print both specs, print workstation.id

All lessons in Introduction To Luau