From 75af8a2ebcc61b32a734fcc61c3b28d8ed2f8735 Mon Sep 17 00:00:00 2001 From: "ajeffrey@roblox.com" Date: Mon, 29 Mar 2021 20:12:15 -0500 Subject: [PATCH] First draft write-up of generics --- .../2021-03-29-luau-recap-march-2021.md | 27 ++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/docs/_posts/2021-03-29-luau-recap-march-2021.md b/docs/_posts/2021-03-29-luau-recap-march-2021.md index 5daab3e0..31dfcc31 100644 --- a/docs/_posts/2021-03-29-luau-recap-march-2021.md +++ b/docs/_posts/2021-03-29-luau-recap-march-2021.md @@ -32,10 +32,30 @@ end ## Generic functions -[TODO] +Luau has always supported type inference for generic functions, for example: +``` +type Point = { x: X, y: Y } +function swap(p) + return { x = p.y, y = p.x } +end +local p : Point = swap({ x = "hi", y = 37 }) +local q : Point = swap({ x = "hi", y = true }) +``` +but up until now, there's been no way to write the type of `swap`, since Luau didn't have type parameters to functions (just regular old data parameters). Well, now you can: +``` +function swap(p : Point): Point + return { x = p.y, y = p.x } +end +``` +Generic functions can be used in function declarations, and function types too, for example +``` +type Swapper = { swap : (Point) -> Point } +``` ## Typechecking improvements +We've made various improvements to the Luau typechecker: + * Check bodies of methods whose `self` has type `any` * More precise types for `debug.*` methods * Mutually dependent type aliases are now handled correctly @@ -62,7 +82,8 @@ Luau now has a shiny new logo! ## Coming soon... +* Generic variadics! * Native Vector3 math with dramatic performance improvements! * Better tools for memory analysis! -* Better treatment of cyclic requires during type checking -* Better type refinements including nil-ability checks, `and`/`or` and `IsA` +* Better treatment of cyclic requires during type checking! +* Better type refinements including nil-ability checks, `and`/`or` and `IsA`!