## 32 lines of goodness, yet another OO lib

ishkabible
Party member
Posts: 241
Joined: Sat Oct 23, 2010 7:34 pm
Location: Kansas USA

### 32 lines of goodness, yet another OO lib

alright, it seems like everyone has made an OO library at 1 point or another. i thought i would try my hand at it. i was inspired by a stack overflow comment(see bottom) that presented a means to create classes via a little domain specific language utilizing Lua's odd 1 argument function syntax. i didn't like his code very much so i scratched it and started over. even after implementing everything i wanted to it ended up being only 32 lines!! i present to you my little OO library.

Code: Select all

local mt_class = {}

function mt_class:extends(parrent)
self.super = parrent
setmetatable(mt_class, {__index = parrent})
parrent.__members__ = parrent.__members__ or {}
return self
end

local function define(class, members)
class.__members__ = class.__members__ or {}
for k, v in pairs(members) do
class.__members__[k] = v
end
function class:new(...)
local newvalue = {}
for k, v in pairs(class.__members__) do
newvalue[k] = v
end
setmetatable(newvalue, {__index = class})
if newvalue.__init then
newvalue:__init(...)
end
return newvalue
end
end

function class(name)
local newclass = {}
_G[name] = newclass
return setmetatable(newclass, {__index = mt_class, __call = define})
end


that's it
here is an example on how to use it

Code: Select all

--creates a class with 2 data members
class "Shape" {
width = 0;
height = 0;
}
--creates a constructor
function Shape:__init(a, b)
self.width = a
self.height = b
end
--creates a function that dose nothing useful,
function Shape:talk()
print("shapes can talk?")
end

--creates a class called rectanlge that inherts from Shape
class "Rectangle" : extends(Shape) {}

--constructor has already been inhierted, no need to make it
function Rectangle:talk()
print("im a rectangle!!")
end
--new function to show how a sub-class can call it's parrents methods even if they have been overloaded
--if you need to call the parrent of a parrents method you can do self.super.super.method(self) and so on untill you have gone back sufficently far
function Rectangle:supertalk()
self.super.talk(self)
end
--create an area function that tells us the area of a rectangle
function Rectangle:area()
return self.width * self.height
end

--creates another class that inherits from shape, this time representing a triangle
class "Triangle" : extends(Shape) {}

function Triangle:area()
return self.width * self.height / 2
end

local rect = Rectangle:new(10, 10)
local tri = Triangle:new(10, 10)

print(rect:area())
print(tri:area())
print(rect:talk())
print(rect:supertalk())

http://stackoverflow.com/questions/8952 ... and-tricks
you can see one of our resident lovers plugging his OO lib, middle class
Last edited by ishkabible on Thu Jul 07, 2011 6:15 pm, edited 1 time in total.
Ensayia
Party member
Posts: 399
Joined: Sat Jun 12, 2010 7:57 pm

### Re: 32 lines of goodness, yet another OO lib

One of these days I'm going to learn what the OOP fuss is all about and probably kick myself a thousand times for not doing it sooner.

About your code: A functional minimalist OOP library in 32 lines? Wow.
BlackBulletIV
Inner party member
Posts: 1261
Joined: Wed Dec 29, 2010 8:19 pm
Location: Queensland, Australia
Contact:

### Re: 32 lines of goodness, yet another OO lib

I have to commend you, that's some very cool syntax you've got there... especially for 32 lines.
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

### Re: 32 lines of goodness, yet another OO lib

BlackBulletIV wrote:I have to commend you, that's some very cool syntax you've got there... especially for 32 lines.
Why you little... I did this in slither ages ago!

Anyway, back on topic, you might want to put it on the wiki or something, so others can easily download and use it. (Or just make a download available.)
BlackBulletIV
Inner party member
Posts: 1261
Joined: Wed Dec 29, 2010 8:19 pm
Location: Queensland, Australia
Contact:

### Re: 32 lines of goodness, yet another OO lib

bartbes wrote:
BlackBulletIV wrote:I have to commend you, that's some very cool syntax you've got there... especially for 32 lines.
Why you little... I did this in slither ages ago!
I was more referring to the Class : extends(SuperClass) syntax, which had me puzzled for a bit, and the other things that are different, like definition of data members inside the brackets (reminds me of Objective-C).

But anyway, I though Slither's syntax was cool as well when I saw it; I just didn't say anything, lol.
ishkabible
Party member
Posts: 241
Joined: Sat Oct 23, 2010 7:34 pm
Location: Kansas USA

### Re: 32 lines of goodness, yet another OO lib

how might i add it to the wiki?

edit:
One of these days I'm going to learn what the OOP fuss is all about and probably kick myself a thousand times for not doing it sooner.
you don't use OOP?!?

why you should use OOP
1) it makes it easy to model objects as it's name suggests. why is this important? becuase a few numbers doesn't really allow you a moulder definition of a enemy
2) it allows you to use the same code for different types!! why is this important? say you have 2 types of enemy's, 1 for zombies and 1 for fire breathing demons, demons behave differently from zombies so how dose one manage them uniformly? you use OOP, inheritance, and polymorphism!! if they both have an update method then you can store the enemy's in the same buffer and they still all update. this is more of a wow factor in statically typed languages but it still has some hold in Lua
3) say you want to have an event listener, something to run code when ever a certain event happens. you are already using a very similar one else where in your code. do you really want to rewrite all that code? of course not, you use inheritance and just over load a few members!!
4) it will make you fill better...ok maybe not but you should really try it. look at Java it's a pure OO language(IMO), if you can't learn OOP from Java you really can't learn OOP.
slime
Solid Snayke
Posts: 2932
Joined: Mon Aug 23, 2010 6:45 am
Contact:

### Re: 32 lines of goodness, yet another OO lib

Object-oriented programming is useful (and a natural direction for code to take), but it is not the answer to everything. Best in moderation, like most things in life.
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Contact:

### Re: 32 lines of goodness, yet another OO lib

ishkabible wrote:... presents its library
Nice! Most of the things you are doing in your lib have already been done in others, but not with so little lines. The smallest OOP lib in the LÖVE is still (basic) SECS, though.
ishkabible wrote:http://stackoverflow.com/questions/8952 ... and-tricks
you can see one of our resident lovers plugging his OO lib, middle class
I can only give, and give, and then give some more.
ishkabible wrote:Look at Java it's a pure OO language(IMO)
That is ostensibly false. Java is OO, but not pure OO.
ishkabible wrote:if you can't learn OOP from Java you really can't learn OOP.
Java's main strengths are its (relative) performance, its ubiquity (lots of people know it) and its extensive library. It's a language you learn to get a job in the programming industry, but it isn't specially good as a learning tool. Or at least, it isn't as good as other alternatives out there.
slime wrote:Object-oriented programming is useful (and a natural direction for code to take), but it is not the answer to everything. Best in moderation, like most things in life.
Indeed. But which one is better: being extremely moderate or moderately extreme ?
When I write def I mean function.
TechnoCat
Inner party member
Posts: 1611
Joined: Thu Jul 30, 2009 12:31 am
Location: Denver, CO
Contact:

### Re: 32 lines of goodness, yet another OO lib

kikito wrote:Java's main strengths are its (relative) performance, its ubiquity (lots of people know it) and its extensive library. It's a language you learn to get a job in the programming industry, but it isn't specially good as a learning tool. Or at least, it isn't as good as other alternatives out there.
Interesting, CS101 at my university is entirely Java.
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

### Re: 32 lines of goodness, yet another OO lib

TechnoCat wrote:
kikito wrote:Java's main strengths are its (relative) performance, its ubiquity (lots of people know it) and its extensive library. It's a language you learn to get a job in the programming industry, but it isn't specially good as a learning tool. Or at least, it isn't as good as other alternatives out there.
Interesting, CS101 at my university is entirely Java.
Same here, I recall friends in the CS department at my old university complaining about Java all the time. They wanted C++, citing it as a "real world language."
Kurosuke needs beta testers

### Who is online

Users browsing this forum: Google [Bot], Majestic-12 [Bot] and 6 guests