Menu
CoddyTech

Recap: Generic Functions

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

challenge icon

Challenge

Medium

Pack two values of different types into one pair.

  1. Write type Pair<T, U> = {first: T, second: U}.
  2. Write local function makePair<T, U>(first: T, second: U): Pair<T, U> returning {first = first, second = second}.
  3. Store makePair("Nova", 87) in local playerScore: Pair<string, number> and print `({playerScore.first}, {playerScore.second})`.

Press Run Code: the output shows (Nova, 87).

Try it yourself

-- 1) type Pair<T, U> = {first: T, second: U}
-- 2) makePair<T, U>(first: T, second: U): Pair<T, U>
-- 3) playerScore from makePair("Nova", 87), then print it
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