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

OOP in lua

Started by mad-murdock, 18 August 2012 - 02:27 PM
mad-murdock #1
Posted 18 August 2012 - 04:27 PM
have you ever wished you have object oriented possibilities in lua? well, its possible.

took me a bit to research, so i decided to leave a link in this very section

http://lua-users.org/wiki/ObjectOrientedProgramming

sure you dont have the full arsenal of full featured OOP languages and miss stuff like polymorphism. but the basics that make up OOP after all are all there.

happy coding
Pharap #2
Posted 18 August 2012 - 04:56 PM
have you ever wished you have object oriented possibilities in lua? well, its possible.

took me a bit to research, so i decided to leave a link in this very section

http://lua-users.org...ntedProgramming

sure you dont have the full arsenal of full featured OOP languages and miss stuff like polymorphism. but the basics that make up OOP after all are all there.

happy coding

For anyone who has ever heard me say OO for lua isn't very clearly explained, this is the webpage/site I'm referring to. It's all there but it's not really written for someone who isn't already used to OO in lua.
Pinkishu #3
Posted 18 August 2012 - 07:15 PM
http://www.lua.org/pil/16.html i liked that explanation
dUc0N #4
Posted 18 August 2012 - 10:07 PM
I'm going to stick with learning OOP with Python and staple the Lua syntax over the top of that conceptual knowledge later on. =-P

Edit: Now that I think about it… should this thread be elsewhere? OOP tutorials aren't quite an API or utility.
Edited on 18 August 2012 - 08:08 PM
FuzzyPurp #5
Posted 19 August 2012 - 06:58 AM
You can find some OOP examples in use in the Redworks addon. If you have Redworks, just type MineDwarf.
Anyway, due to the content in the OP, thread will be moved to the General Section.
immibis #6
Posted 19 August 2012 - 08:31 AM
How can't you have polymorphism?


Superclass = {}

function Superclass:create()
  return setmetatable({}, {__index = Superclass})
end

function Superclass:callPrintStuff()
  self:printStuff()
end

function Superclass:printStuff()
  print("Superclass printStuff")
end



Subclass = {}
setmetatable(Subclass, {__index = Superclass})

function Subclass:create()
  return setmetatable({}, {__index = Subclass})
end

function Subclass:printStuff()
  print("Subclass printStuff")
end


local inst = Subclass:create()
Subclass:callPrintStuff() -- prints Subclass printStuff