Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.
 Entire forum ➜ MUSHclient ➜ Lua ➜ OOP method and class variable collisions

OOP method and class variable collisions

It is now over 60 days since the last post. This thread is closed.     Refresh page


Posted by Jedhi   (37 posts)  Bio
Date Fri 25 Jan 2013 03:12 AM (UTC)

Amended on Fri 25 Jan 2013 03:13 AM (UTC) by Jedhi

Message

animals = {}

function animals:clone ()
  local new_table = {}
  
  for k, v in pairs (self) do
    new_table[k] = v
  end
  
  return new_table
end

function animals:setup (fields)
  self.fields = fields
end

function animals:display()
  tprint (self)
end

dog = animals:clone()
dog.name = "Harry"
dog.age = "5"

dog:display()


in this example everything works fine, but if i had there a method :name i could not assigne animal its name.

i see 2 solutions. add _ to every class method like

animals:_display() and animals:_name()

and store all animal details like that

dog.details.name = "Harry"

which approach is better?
Top

Posted by Nick Gammon   Australia  (23,102 posts)  Bio   Forum Administrator
Date Reply #1 on Fri 25 Jan 2013 05:21 AM (UTC)
Message
It's not clear what you are attempting to do here. Try reading the Programming in Lua book (available online for downloading) which goes into some of the object-oriented aspects of Lua.

http://www.lua.org/pil/

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Fiendish   USA  (2,533 posts)  Bio   Global Moderator
Date Reply #2 on Fri 25 Jan 2013 06:16 AM (UTC)

Amended on Fri 25 Jan 2013 06:18 AM (UTC) by Fiendish

Message
This question actually has nothing to do with OOP. He's just complaining about how Lua lets you assign data over a function.

Quote:
which approach is better?

Whichever way makes you happy.

https://github.com/fiendish/aardwolfclientpackage
Top

Posted by Fadedparadox   USA  (91 posts)  Bio
Date Reply #3 on Fri 25 Jan 2013 11:26 PM (UTC)
Message
Best? Neither. Use unique names. That said, do whichever you prefer. Functions are not different than text, numbers, boolean, or tables. They're all variables.
Top

Posted by Nick Gammon   Australia  (23,102 posts)  Bio   Forum Administrator
Date Reply #4 on Sat 26 Jan 2013 02:41 AM (UTC)
Message
Quote:

in this example everything works fine, but if i had there a method :name i could not assigne animal its name.


Are you using the same namespace (table) for both data and methods? If so, you might want to make another table (eg. properties) and put everything under that. That only stops you having a method called "properties" but everything else is then open for use.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Anaristos   USA  (24 posts)  Bio
Date Reply #5 on Sun 27 Jan 2013 08:13 AM (UTC)
Message
Here is how I do OOP in Lua:

http://pastebin.com/9n8b2PtF

Feel free to use the code any way you want.

Sic itur ad astra
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


18,193 views.

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.