I’m trying to write a module that generates a stack of random numbers within certain bounds, but I want to define a type that indicated two number keys each equal to a number.
Code here:
local RandomNums = {}
RandomNums.__index = RandomNums
local StackSizeFallback = 100
local RandomGen = Random.new()
type boundsType = {
[1]: number,
[2]: number,
}
function RandomNums.GenerateStack(stackSize: number?, bounds: boundsType)
if not bounds then error("Bounds not given") return end
local stack = {}
if not stackSize then
stackSize = StackSizeFallback
end
for i = 1, stackSize do
stack[i] = RandomGen:NextInteger(table.unpack(bounds))
end
return setmetatable(stack, RandomNums)
end
function RandomNums:clearStack()
table.clear(self)
return self
end
return RandomNums
But when defining ‘boundsType’ I get an error. I’ve seen this done fine with dictionaries as in:
type animals = {
cat: string,
dog: string,
}
Or with a single key for an array like:
type animals = {
[number]: number
}
Any ideas?