Generic's in Lua, how to use them?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I only want to understand Generics in Lua (and not the for generic)

  2. What is the issue? Include screenshots / videos if possible!
    I can‘t find something in google about this, so i am asking here

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I not find that what i am searching

I have only found this, but not really was helpfull as i not really understand:

So i was wondering on how it work in Roblox, in Lua.


Generics represent whatever type is input

type array<T> = {[number]:T}

So with this type, keys are numbers and values are of type T

-- T is number in the case, so values are numbers
local a:array<number> = {}
a[1] = 1 -- ok
a[2] = 2 -- ok
a[3] = "3" -- warns, "3" isn't a number
-- T is array<number>, so keys are numbers and values are tables with keys and
-- values as numbers
local b:array<array<number>> = {}
b[1] = {}
b[2] = {}
b[3] = {}
b[1][1] = 1 -- ok
b[2][1] = 2 -- ok
b[3][1] = "3" -- warns, "3" isn't a number

Generics exist so you don’t have to create a new type for every type which would be used.

type numArray = {[number]:number}
type strArray = {[number]:string}
type boolArray = {[number]:boolean}
-- and so on

That actually helped a lot, I’ve been staying away from generics since they scare me whenever I try to understand them. In a way, it’s similar to how Java implements classes (I hate Java, hence why I cringe when I see this).

It’s actually a lot nicer than how one would define a type in C or C++ (well, outside of structs, anyways). Thanks for clearing this up, even though I’m not the OP lol. @Eternalove_fan32 I hope this helped you too!

1 Like

Thanks to you all, it really helped me out! But i have now other questions: Why i can‘t find a tutorial about this, @Halalaluyafail3? Else thanks, i know Java so i know how to use them in the Java language, but i was scared of the idea that it was different from Java and that i have need to re-learn it. @tralalah, yes, it helped me out.

1 Like

Ok, sorry to revive this topic, but since Luau was published, I dare to ask this question:

How do FunctionTypes work? I’ve read their syntax on the official Luau website, but other than that, I haven’t read any example for now where you actually use it. I tried to use it all the time, but no method works. How to use it? A function type is used, it’s not there as a decoration, you use this one after all. How then? I ask it, because I would like to use generics ^^

Thanks for reading it:

--function-type example
type Test<A> = (A) -> A

--Nice, i have my function-type

local function Test() --, but how to deploy these?


Ik that i just could do this:

local function Test(a:any)
    return a

, but i want to use generics and know, how to use function-types

You can declare a variable with a type and then assign to it with the function.

type Test<A> = (A) -> A
local TestValue:Test<number>
function TestValue(x:number):number
	return x*2

This doesn’t seem particularly useful though, because in the function definition it should have the same type.

1 Like

Ok, thanks anyway! 30 charsssss