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!
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.
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:
~~Eterna
--function-type example
type Test<A> = (A) -> A
--Nice, i have my function-type
local function Test() --, but how to deploy these?
end
Ik that i just could do this:
local function Test(a:any)
return a
end
, but i want to use generics and know, how to use function-types