How can I tell this function that it returns a metatable?

Hi,

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
2 Likes

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.

Not really an issue, just asking how I can make this type a metatable, but thats not really how that works.

There isn’t really a way (that I know of) that does what you want cus one thing stops the other from working so it won’t be so good.

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.

2 Likes

Bad to do this.
That disables the autocomplete for methods so that didn’t fix anything.

That’s exactly what (s)he’s trying to avoid.

Forget that I managed to fix it. Try this:

--!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
1 Like

I don’t know what is happening on your end, but autocomplete works perfectly fine.

But isn’t that what’s exactly in the post?

Weird.

I don’t get what you mean by this but your avoiding the usage of --!strict and:

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.

Fixed the ordering of things, but using global variables don’t cause warnings?

To be more specific:

var = {}        -- Global Variable
local Item = {} -- Local Variable

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)
1 Like

That seems to work, as im no longer getting any warnings, but im not entire sure if that is the best way to do it.

Edit: Not really, got the same warning again.

What warnings? I had 0.
There’d no other way to do this but if you find one do let me know.

This works

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()

Well, I cannot test this rn, so I’ll have to do it later.
Also, I wasn’t asking for wntire code.

Doesnt really seem to work, but this is what I want to do:

Change the function from saying this:

example(...): { @metatable module, {...} }

to saying this:

example(...): SomeData -- Looks much cleaner

but since the item its returning is a metatable, and not an ordinary table, im not sure how I can do this change.

You should explain what doesn’t work because it’s a little hard to help you otherwise.

“SomeData” can be a type you define somewhere in your code, doesn’t matter where but you have to define it. Something like

type SomeData = typeof(setmetatable(ClassPrototype(), Object.Metatable))

somewhere at the bottom also works.