Skip to content

Commit 281f653

Browse files
authored
Mention generic defaults in type checking docs (#56)
1 parent aec0df2 commit 281f653

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

_pages/typecheck.md

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -237,10 +237,14 @@ Luau supports a concise declaration for array-like tables, `{T}` (for example, `
237237
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`.
238238

239239
```lua
240-
type Pair<T> = {first: T, second: T}
240+
type Pair<T> = {first: T, second: T }
241+
-- generics can also have defaults!
242+
type PairWithDefault<T = string> = Pair<T>
241243

242-
local strings: Pair<string> = {first="Hello", second="World"}
243-
local numbers: Pair<number> = {first=1, second=2}
244+
local strings: Pair<string> = { first="Hello", second="World" }
245+
local numbers: Pair<number> = { first=1, second=2 }
246+
-- can just treat PairWithDefault as a type that doesnt have generics because it has all of its generics assigned a default!
247+
local more_strings: PairWithDefault = { first = "meow", second = "mrrp" }
244248
```
245249

246250
## Generic functions
@@ -275,6 +279,12 @@ for example the type of two-argument `table.insert` is:
275279
```lua
276280
<T>({T}, T) -> ()
277281
```
282+
Note: Functions don't support having defaults assigned to generics, meaning the following is invalid
283+
```lua
284+
function meow<T = string>(mrrp: T)
285+
print(mrrp .. " :3")
286+
end
287+
```
278288

279289
## Union types
280290

0 commit comments

Comments
 (0)