Using a Generic Function
Part of the Introduction To Luau section of Coddy's Lua journey. Lesson 63 of 80.
You never tell identity what T is: the checker works it out on its own. This is type inference: at every call site, Luau looks at the argument and solves for T:
local word = identity("Luau") -- T inferred as string
local count = identity(12) -- T inferred as number
local flag = identity(false) -- T inferred as booleanEach call is independent: T can be a string in one line and a number in the next. The inferred result is just as safe as a hand-written annotation: word is a string to the checker, with full autocomplete and error checking.
One important difference from TypeScript: TS lets you force the type at the call site with identity<number>(5). Luau has no call-site type arguments. That line isn't special syntax, so Luau reads the angle brackets as less-than/greater-than comparisons and the code fails. When you want to state the type explicitly, annotate the variable instead:
local count: number = identity(12)You can watch inference at work with a familiar tool: typeof returns the runtime type name of a value, so print(typeof(identity(12))) prints number: the value went through the generic function completely unchanged.
Challenge
EasyLet the checker work out T at each call.
- Under the function, store
identity("Oslo")inlocal cityandidentity(42)inlocal floors, with no annotations. - Print
typeof(city), thentypeof(floors).
Press Run Code: the output shows string and number.
Try it yourself
-- The generic identity function from the last lesson
local function identity<T>(value: T): T
return value
end
-- 1) city and floors from identity calls, no annotations
-- 2) print typeof of each
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 ValuesDefaults for BooleansVariadic FunctionsDefining Function TypesRecap: Typed Functions2Core Types
Number, String and BooleanThe '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 TablesReading a GridBuilding a Gridtable.unpackRecap: 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 FunctionsPractice on your own: Online Luau compiler