Menu
Coddy logo textTech

Recap: Fine-Tuning Types

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

challenge icon

Challenge

Medium

Build a small configuration processor.

  • Declare type Config = {[string]: string | number | boolean}.
  • Write valueType(config: Config, key: string): string that returns missing if the key's value is nil, otherwise string, number or boolean according to typeof.
  • Write processValue(config: Config, key: string): string that uses valueType, then returns:
    • the value uppercased (string.upper, cast with :: string) for strings
    • tostring of the value times 10 (cast with :: number) for numbers
    • tostring of the opposite value (cast with :: boolean) for booleans
    • missing when the key isn't there
  • Create settings: Config with appName = "MyApp", version = 2.5, debugMode = true, maxUsers = 100 and theme = "dark".

Then print, each on its own line:

  1. valueType(settings, "appName")
  2. valueType(settings, "version")
  3. valueType(settings, "debugMode")
  4. valueType(settings, "timeout")
  5. processValue(settings, "theme")
  6. processValue(settings, "maxUsers")
  7. processValue(settings, "debugMode")
  8. processValue(settings, "timeout")

Try it yourself

-- Write code here
-- 1) type Config = {[string]: string | number | boolean}
-- 2) valueType(config, key): "missing" / "string" / "number" / "boolean"
-- 3) processValue(config, key): upper / *10 / opposite / "missing"
--    (guard with valueType first, then cast with ::)
-- 4) build the settings table and print the eight test calls

All lessons in Introduction To Luau