This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
tesla1889's profile picture

adding object API functions to object metatables

Started by tesla1889, 25 February 2013 - 05:48 AM
tesla1889 #1
Posted 25 February 2013 - 06:48 AM
i know that the string API functions are in the default string metatable (or at least partially)

local str = "test"
str:len() == 4 --> true
str:sub(1,1) == "t" --> true
str:find("e") == 2 --> true
and this makes string manipulation a lot easier

my request is that the rest of the string API functions be added to the string metatable, the table API functions be added to the table metatable, and the coroutine API functions be added to the coroutine metatable
Cloudy #2
Posted 25 February 2013 - 06:55 AM
What? Please run that by me again. How would this be useful?
tesla1889 #3
Posted 25 February 2013 - 07:00 AM
its OOP

instead of table.insert(t,"something")
it would be t:insert("something")

instead of string.match(s,"%p")
it would be s:match("%p")

instead of coroutine.resume(c,…)
it would be c:resume(…)
Cloudy #4
Posted 25 February 2013 - 07:06 AM
Yeah, I don't see the point. We're not going to fiddle about with default Lua stuffs' metatables. The Lua functions have the advantage of being the same across the programs/interpreters you use. String table already works like that.
Eric #5
Posted 25 February 2013 - 07:26 AM
That's not possible for tables, since you end up overwriting the methods with your keys.
tesla1889 #6
Posted 25 February 2013 - 09:47 AM
That's not possible for tables, since you end up overwriting the methods with your keys.
its the metatable, not the table itself
so yes. you could. but that wouldnt help you at all
Eric #7
Posted 25 February 2013 - 10:49 AM
That's not possible for tables, since you end up overwriting the methods with your keys.
its the metatable, not the table itself
so yes. you could. but that wouldnt help you at all

Ok, take this example:

local openDoorFor = {
    Eric = true,
    Notch = true
}

if openDoorFor[somePlayerName] then
    -- ...
end

Ok, so what if `somePlayerName` is 'insert' or 'remove'. This is why raw tables cannot themselves have metatables.
theoriginalbit #8
Posted 25 February 2013 - 03:09 PM
instead of string.match(s,"%p") it would be s:match("%p")
Idk what you're doing wrong somewhere, but I have not come across a Lua string function that you cannot use colon notation instead of dot notation; except for functions that are not modifying or accessing a string; obviously there is no point to having string.char, which expects a number, and string.dump, which expects a function, being able to use the colon notation; I should point out that you can still use colon notation on it, it just errors those functions because you are passing the functions a string when they want a number or function. all these work

if its string.<func>(<the string>, <other-params>) then you can do it with : because of the first 'self' parameter.
tesla1889 #9
Posted 25 February 2013 - 06:26 PM
–snip–
Ok, so what if `somePlayerName` is 'insert' or 'remove'. This is why raw tables cannot themselves have metatables.

they can have metatables. its just that if you set the index "insert" for a table, you cant use that method on the table anymore
immibis #10
Posted 26 February 2013 - 12:42 AM
–snip–
Ok, so what if `somePlayerName` is 'insert' or 'remove'. This is why raw tables cannot themselves have metatables.

they can have metatables. its just that if you set the index "insert" for a table, you cant use that method on the table anymore
They can't have metatables by default. You're welcome to set your own by calling setmetatable.

Also,
I'm not changing the behaviour of stock lua librarys
dan200 #11
Posted 26 February 2013 - 12:45 AM
I'm not changing the behaviour of stock lua librarys
Skullblade #12
Posted 26 February 2013 - 12:53 AM
Immbis u quoted from the future….woah
Cloudy #13
Posted 26 February 2013 - 04:09 AM
No point to having this open now :)/>