What is the __type metamethod?

I was reading up on metatables when I saw this:

local vector2 = {__type = "vector2"}

__type isn’t documented in the metamethods list. What does it do? Are there other undocumented metamethods out there?

4 Likes

Do you mean typeof?

This can be found in the Roblox Globals Page in the Developer Hub.

When I do this:

local Test = {__type = "test"}

print(typeof(Test))

It prints “table”

1 Like

@SpaceDice999 where do you read this, i not am seeing this on the site, so can‘t help.

But i think that it allow you to use the type(object) function, if yes so use pls typeof, it was better as the type function So, normally, if you then make this:

print(type(YourClass))

It should return vector2, so the string you have write

It’s not a metamethod, this is just a variable in the table and the variable is named ‘__type’.

5 Likes

The reason it prints that is because

{__type = "test"}

is a table (hence the curly brackets)

1 Like

Yes, i am good with this, and i understand really good OOP, but i not understand that it was a variable, why and how? So, my question is when use this?

You use it whenever you want.
You can assign values to it and read it.

local tab = {__type = "test"}
print(tab.__type) -- test
tab.__type = 4
print(tab.__type) -- 4

just like a normal variable, there’s no difference, because __type is a normal variable, it has nothing to do with metamethods and metatables, except that its name is similiar to the metamethod’s names

local tab = {a = "test"}
print(tab.a) -- test
tab.a = 4
print(tab.a) -- 4
3 Likes

In this case __type isn’t a true metamethod, but rather a value dictating a custom “type” of the custom “class”.

image

There is actually a __type metamethod, its just you can’t use it, or it just appears you cant.

image
image

7 Likes

Also, can’t we use the function “typeof()” or “type” with it? Pity. @EpicMetatableMoment , I just want to say that if it would be so, then we can use the new luau instead of this value. But that depends on the user. If you are interested, here is a link for you, hope it help.

it is a metamethod dude roblox uses it

It’s not a metamethod. Look at the documentation.

The code example that used __type explicitly checks it.

It’s literally just a table property with a similar naming scheme as a metamethod

look in the roblox source code bro its right there


it is used by roblox see lol

As explained to me by someone through DMs, the __type metamethod is not usable or accessible to developers. Therefore, my statement still holds true.


Because in the code sample, that’s all it is. Create a metatable with __type and try calling type or typeof with it

1 Like

you said its a table property and it is usable to developrs and we dont know what someone explains to you through dms so my point still stands

if its not acccessible to devs how come i was able to read it, every roblox instance and class has it

While it is a metamethod in Vanilla Lua, it is disabled in Luau. Period.

It is probably only effective on a specific type of userdata