Jump to content




adding object API functions to object metatables


  • This topic is locked This topic is locked
12 replies to this topic

#1 tesla1889

  • Members
  • 351 posts
  • LocationSt. Petersburg

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

#2 Cloudy

    Ex-Developer

  • Members
  • 2,543 posts

Posted 25 February 2013 - 06:55 AM

What? Please run that by me again. How would this be useful?

#3 tesla1889

  • Members
  • 351 posts
  • LocationSt. Petersburg

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

#4 Cloudy

    Ex-Developer

  • Members
  • 2,543 posts

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.

#5 Eric

  • Members
  • 92 posts
  • LocationUK

Posted 25 February 2013 - 07:26 AM

That's not possible for tables, since you end up overwriting the methods with your keys.

#6 tesla1889

  • Members
  • 351 posts
  • LocationSt. Petersburg

Posted 25 February 2013 - 09:47 AM

View PostEric, on 25 February 2013 - 07:26 AM, said:

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

#7 Eric

  • Members
  • 92 posts
  • LocationUK

Posted 25 February 2013 - 10:49 AM

View Posttesla1889, on 25 February 2013 - 09:47 AM, said:

View PostEric, on 25 February 2013 - 07:26 AM, said:

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.

#8 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 25 February 2013 - 03:09 PM

View Posttesla1889, on 25 February 2013 - 07:00 AM, said:

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.

#9 tesla1889

  • Members
  • 351 posts
  • LocationSt. Petersburg

Posted 25 February 2013 - 06:26 PM

View PostEric, on 25 February 2013 - 10:49 AM, said:

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

#10 immibis

    Lua God

  • Members
  • 1,033 posts
  • LocationWellington, New Zealand

Posted 26 February 2013 - 12:42 AM

View Posttesla1889, on 25 February 2013 - 06:26 PM, said:

View PostEric, on 25 February 2013 - 10:49 AM, said:

--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,

View Postdan200, on 26 February 2013 - 12:45 AM, said:

I'm not changing the behaviour of stock lua librarys


#11 dan200

  • Administrators
  • 542 posts
  • LocationCambridge, England

Posted 26 February 2013 - 12:45 AM

I'm not changing the behaviour of stock lua librarys

#12 Skullblade

  • Members
  • 470 posts
  • LocationThe Big Apple, NY

Posted 26 February 2013 - 12:53 AM

Immbis u quoted from the future....woah

#13 Cloudy

    Ex-Developer

  • Members
  • 2,543 posts

Posted 26 February 2013 - 04:09 AM

No point to having this open now :)





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users