I tried my best to understand what they are but couldn’t get a single hint of what it is.
My hypothesis that they are just the equivalent of typeof(“function parameter here”) put in <> but I’m obviously wrong
I tried my best to understand what they are but couldn’t get a single hint of what it is.
My hypothesis that they are just the equivalent of typeof(“function parameter here”) put in <> but I’m obviously wrong
Generics let you specify a type which is passed down as a parameter to wherever that parameter may be used. They exist so that you don’t have to create a new type for every single type annotation that you need to use.
So instead of
type numberArray = {number}
type stringArray = {string}
type boolArray = {boolean}
local function test1(arg: numberArray)
-- insert code
end
local function test2(arg: stringArray)
-- insert code
end
local function test3(arg: boolArray)
-- insert code
end
you can just do:
type array<T> = {T}
local function test1(arg: array<number>)
-- insert code
end
local function test2(arg: array<string>)
-- insert code
end
local function test3(arg: array<boolean>)
-- insert code
end
And as for generic functions, I’m not totally sure. But from what I can see, it allows you to pass down specific types for function parameters (if the function returns a value).
local function test<T>(array: {T}): {T}
-- do stuff with array
return array
end
local num: {string} = test({5, 3}) -- Type `{number}` could not be converted to `{string}`
You can read more about them in these posts:
Generic’s in Lua, how to use them? - Help and Feedback / Scripting Support - DevForum | Roblox
Typechecking generics don’t make sense - Help and Feedback / Scripting Support - DevForum | Roblox