So , Im wondering how I could say a type is a metatable, as when you return a metatable, it would look something like this in script:
{ @metatable a, {- -} }
-- when making a metatable using `setmetatable`
-- it tells you that it returns this item which
-- is the metatable.
So, I’m wondering how I can do this in this script? If its even possible?
Because when using --!strict, it gives you warnings about how things are returning when you give it something to expect.
--!strict
type SomeData = {} -- How can I make this type a metatable?
local module = {} -- module table
function module.example(): SomeData -- function 'returns' the custom type
local self = setmetatable({}, {__index = module})
self.self = self -- lol
return self -- using --!strict gives me a warning about the return
end
Would the issue still happen if you changed the name of the local variable? (from “self” to somthing else)
Probably isn’t the isssue, but figured I’d suggest it.
local module = {} -- module table
function module.example(): SomeData -- function 'returns' the custom type
local self = setmetatable({}, {__index = module})
self.self = self -- lol
return self -- using --!strict gives me a warning about the return
end
type SomeData = typeof(module.example())
I believe this should get the script editor to play nicely with the metatable.
--!strict
local Class = {}
Class.__index = Class
type Data = {
a: number,
b: number,
}
type Class = typeof(setmetatable(({}), Class)) & Data
function Class.new(a: number, b: number): Class
return setmetatable({
a = a,
b = b,
}, Class) :: Class
end
function Class.Sum(self: Class): number
return self.a + self.b
end
local class = Class.new(2, 2)
class:Sum()
return Class
The code wont recognize the module.example because it is trying to fire something that doesnt exist yet, and using Global Variables also causes Warnings, which is what I want to avoid
After Edit:
I don’t think that would work with arguments.
There are no global variables instantiated in the code at all.
Correct, it does need a little bit more modification.
--!strict
local module = {} -- module table
function module.example(x : number, y: number): SomeData -- function 'returns' the custom type
local self = setmetatable({}, {__index = module})
self.x = x -- lol
self.y = y
return self -- using --!strict gives me a warning about the return
end
type SomeData = typeof(module.example(table.unpack(...)))
local a = module.example(2, 3)
print(a.x)
print(a.y)
local function ClassPrototype() -- You define all the properties you want your class to have here
return {
Property = true;
}
end
local Object = {}
Object.Class = {}
Object.Metatable = {__index = Object.Class}
function Object.new(): typeof(setmetatable(ClassPrototype(), Object.Metatable))
local NewObject = ClassPrototype()
return setmetatable(NewObject, Object.Metatable)
end
function Object.Class:Method()
end
local NewObject = Object.new()
local Property = NewObject.Property
NewObject:Method()