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?
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?
When I do this:
local Test = {__type = "test"}
print(typeof(Test))
It prints “table”
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’.
The reason it prints that is because
{__type = "test"}
is a table (hence the curly brackets)
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
In this case __type
isn’t a true metamethod, but rather a value dictating a custom “type” of the custom “class”.
There is actually a __type
metamethod, its just you can’t use it, or it just appears you cant.
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
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
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