A Generic Identity Function
Part of the Introduction To Luau section of Coddy's Lua journey — lesson 58 of 73.
Here's the identity function done right — as a generic function:
local function identity<T>(value: T): T
return value
endThe <T> after the function name declares a type parameter — a placeholder that stands for "some type, decided at each call". Inside the signature you use T exactly like a real type: the parameter is a T, and the function returns a T.
Using the same T for input and output is the whole trick — it ties them together. Call identity("hello") and the checker decides T = string for that call, so the result is a string. Call identity(42) and T = number, so the result is a number. One body, every type, nothing lost.
T is only a conventional name (short for "Type") — <Item> or <Value> work just as well. And as always in Luau, the angle brackets exist for the checker only: at runtime this is the plain Lua function function identity(value) return value end.
Challenge
EasyCreate a generic function named identity that:
- declares a type parameter
T - accepts one parameter named
valueof typeT - returns that value, with return type
T
Then create these typed variables using your function:
luckyNumberof typenumber— callidentitywith7greetingof typestring— callidentitywith"Hello, Luau!"isReadyof typeboolean— callidentitywithtrue
Print the following, each on its own line:
luckyNumbergreetingisReady- the result of calling
identitywith"generics"directly insideprint - the result of calling
identitywith99directly insideprint
Try it yourself
-- Write code here
-- 1) define identity<T>(value: T): T
-- 2) create luckyNumber, greeting and isReady with identity calls
-- 3) print them, then print identity("generics") and identity(99)
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
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 Combos3Typed 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 Maps6Typing Table Shapes
Inline Shape AnnotationsType Aliases for ShapesOptional PropertiesShapes vs. Loose TablesExtending ShapesAdding Methods to ShapesSelf and Colon MethodsRecap: Defining Table Shapes9Generics: A First Look
The Problem Generics SolveA Generic Identity FunctionUsing a Generic FunctionGeneric ArraysGeneric Type AliasesRecap: Generic Functions