Object and tinyobj - tiny, fast, prototyping object systems

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
MattRB
Prole
Posts: 11
Joined: Tue Sep 25, 2012 8:16 pm

Object and tinyobj - tiny, fast, prototyping object systems

Post by MattRB »

Tinyobj and Object are two very tiny object systems for Lua.

https://github.com/MatthewBlanchard/Object

Code: Select all

--[[
Copyright (c) 2012 Matthew R. Blanchard

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-]]

Object = {}
Object.__index = Object
setmetatable(Object, Object)

function Object:__call(...)
    return self:new(...)
end

function Object:init()
end

function Object:parent()
    return getmetatable(self)
end

function Object:new(...)
    local o = {}
    setmetatable(o, self)
    self.__index = self
    self.__call = Object.__call
    o:init(...)
    return o
end
It's small and fast and consistently updated. The Object.lua file is larger than this at about 2kb and implements metamethod inheritence and pretty syntax for defining constructors. Here have an example:

Code: Select all

require "Object"

Cat = Object() -- Or Object:new(), Object() is just a shortcut.

function Cat:__tostring()
    return "A " .. self.breed .. " cat"
end

function Cat:Cat() -- New supported syntax for defining constructors. Can also use __init or init.
    self.breed = "mixed" -- Could also do just "Cat.breed = 'blah'" outside function scope.
end


Siamese = Cat()

function Siamese:Siamese()
    self.breed = "Siamese"
end

Bill = Cat()
Phil = Siamese()

print(Bill, Phil)
Last edited by MattRB on Sun Oct 28, 2012 1:15 am, edited 3 times in total.
User avatar
Inny
Party member
Posts: 652
Joined: Fri Jan 30, 2009 3:41 am
Location: New York

Re: Object and tinyobj - tiny, fast, prototyping object syst

Post by Inny »

Looks great, I tend to write my own class systems and they're more or less the same as what you have there. I'm sure if you look over Middleclass and some of the other class systems, they all follow the same principles as well.

One small thing: the term "parent" is a bit overloaded. It's generally used as the name of a node on a tree that has children nodes, which is exactly how you'd describe a class in your hierarchy, but it'd clash with any user defined tree setup. A better name, which tends to not to clash with most user names for things, would be "super".
User avatar
Xgoff
Party member
Posts: 211
Joined: Fri Nov 19, 2010 4:20 am

Re: Object and tinyobj - tiny, fast, prototyping object syst

Post by Xgoff »

Inny wrote:Looks great, I tend to write my own class systems and they're more or less the same as what you have there. I'm sure if you look over Middleclass and some of the other class systems, they all follow the same principles as well.

One small thing: the term "parent" is a bit overloaded. It's generally used as the name of a node on a tree that has children nodes, which is exactly how you'd describe a class in your hierarchy, but it'd clash with any user defined tree setup. A better name, which tends to not to clash with most user names for things, would be "super".
'super' might clash with a mario object, though
User avatar
T-Bone
Inner party member
Posts: 1492
Joined: Thu Jun 09, 2011 9:03 am

Re: Object and tinyobj - tiny, fast, prototyping object syst

Post by T-Bone »

I typically write my own class system too, but I'm moving more towards just using tables (with no metatables anywhere) straight up as they are for simplicity. It is not always as memory efficient but in my experience that rarely matters.
MattRB
Prole
Posts: 11
Joined: Tue Sep 25, 2012 8:16 pm

Re: Object and tinyobj - tiny, fast, prototyping object syst

Post by MattRB »

Updated the first post. Project put on github and updated.
User avatar
Inny
Party member
Posts: 652
Joined: Fri Jan 30, 2009 3:41 am
Location: New York

Re: Object and tinyobj - tiny, fast, prototyping object syst

Post by Inny »

Actually, I'm fascinated by two specific alternatives to traditional class systems:

1. Prototypical Inheritance, which is already built into Lua, it's what we all use to emulate classes, except there's no distinction between a Class object and an Instance Object. There's no "constructor," in the implicitly-called sense, but rather you build up the object beforehand and then set its inheritance chain. Basically that would look like Child = { attributes }; setPrototype( Child, Parent ), or more succinctly Child = Parent:clone { attributes }. It's fairly close to how things were done in the Apple Newtonscript language, of which Lua is a kind of cousin to. This form isn't very popular, despite the fact that it's a primary mechanism in Javascript as well, but only used to replicate classes.

2. Composition Inheritance, usually referred to as Mixins. These make up for the lack of Multiple Inheritance in Single Inheritance schemes, where a class can inherit from many different modules, with the understanding that there will be no implicit relationships adhered to, other than the attributes and functions of the module being copied into the class. This is a builtin feature of Ruby, which is why I kept calling it a Module, instead of just a table.

That second form, Mixins, might make a great feature to include in any class system, and Middleclass supports it if I'm not mistaken.
MattRB
Prole
Posts: 11
Joined: Tue Sep 25, 2012 8:16 pm

Re: Object and tinyobj - tiny, fast, prototyping object syst

Post by MattRB »

I added mixins, it was silly simple.
Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests