From df094792b48d38c44ffd5798193e03bd8ebef544 Mon Sep 17 00:00:00 2001 From: "ajeffrey@roblox.com" Date: Mon, 29 Mar 2021 17:24:26 -0500 Subject: [PATCH] First draft write-up of typed variadics --- .../2021-03-29-luau-recap-march-2021.md | 24 +++++++++++++++++-- 1 file changed, 22 insertions(+), 2 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 641de87c..1209b2d0 100644 --- a/docs/_posts/2021-03-29-luau-recap-march-2021.md +++ b/docs/_posts/2021-03-29-luau-recap-march-2021.md @@ -9,7 +9,26 @@ Luau is our new language that you can read more about at [https://roblox.github. ## Typed variadics -[TODO] +Luau supports *variadic* functions, meaning ones which can take a variable number of arguments (varargs!) but previously there was no way to specify their type. Now you can! +``` +function f(x: string, ...: number) + print(x) + print(...) +end +f("hi") +f("lo", 5, 27) +``` +This function takes a string, plus as many numbers as you like, but if you try calling it with anything else, you'll get a type error, for example `f("oh", true)` gives an error " Type `boolean` could not be converted into `number`" + +Variadics can be used in function declarations, and function types, for example +``` +type T = { + sum: (...number) -> number +} +function f(x: T) + print(x.sum(1, 2, 3)) +end +``` ## Generic functions @@ -39,5 +58,6 @@ Luau now has a shiny new logo! ## Coming soon... -* Better treatment of cyclic requires +* Generic variadics! +* Better treatment of cyclic requires! * [Anything else?]