From 3286f78ba0fcd6d2cca27f9f6652fc3ac14d9c02 Mon Sep 17 00:00:00 2001 From: lynn <62822174+gaymeowing@users.noreply.github.com> Date: Tue, 12 Aug 2025 11:25:38 -0400 Subject: [PATCH] Mention generic defaults in type checking docs --- _pages/typecheck.md | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/_pages/typecheck.md b/_pages/typecheck.md index f3ef52a..81cb97b 100644 --- a/_pages/typecheck.md +++ b/_pages/typecheck.md @@ -237,10 +237,14 @@ Luau supports a concise declaration for array-like tables, `{T}` (for example, ` The type inference engine was built from the ground up to recognize generics. A generic is simply a type parameter in which another type could be slotted in. It's extremely useful because it allows the type inference engine to remember what the type actually is, unlike `any`. ```lua -type Pair = {first: T, second: T} +type Pair = {first: T, second: T } +-- generics can also have defaults! +type PairWithDefault = Pair -local strings: Pair = {first="Hello", second="World"} -local numbers: Pair = {first=1, second=2} +local strings: Pair = { first="Hello", second="World" } +local numbers: Pair = { first=1, second=2 } +-- can just treat PairWithDefault as a type that doesnt have generics because it has all of its generics assigned a default! +local more_strings: PairWithDefault = { first = "meow", second = "mrrp" } ``` ## Generic functions @@ -275,6 +279,12 @@ for example the type of two-argument `table.insert` is: ```lua ({T}, T) -> () ``` +Note: Functions don't support having defaults assigned to generics, meaning the following is invalid +```lua +function meow(mrrp: T) + print(mrrp .. " :3") +end +``` ## Union types