Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion

30log : a thirty-lines object-orientation framework.

edited January 2015 in Code Sharing Posts: 9

30log, in extenso 30 Lines Of Goodness is a minified framework for object-orientation in Lua. It features class creation, object instantiation, single inheritance and a basic support for mixins.

And, it makes 30 lines. No less, no more.
Well, quite 30 lines, actually...

30log was designed to be very simple to use.


local class = require '30log'
-- An entity class
local Entity = class ("Entity",{x = 0, y = 0})
-- A "player" class that inherits from "entity" class
local Player = Entity:extend({karma = 0})
-- Player class constructor
function Player:init(x, y, karma)
self.x, self.y, self.karma = x, y, karma
end
-- A method to move a "player" object
function Player:moveTo(x,y)
self.x, self.y = x,y
end
-- Spawns an instance from player class.
local blob = Player(10, 10, 2)
print(blob.x, blob.y, blob.karma) --> 10, 10, 2
-- Moves the blob
Blob:moveTo(25, 25)
print(blob.x, blob.y, blob.karma) --> 25, 25, 2

You can through the quicktour tutorial included in the Readme to see the full list of features available.

Feel free to give it a spin!

Tagged:

Comments

  • IgnatzIgnatz Mod
    Posts: 5,396

    So let me get this right. This is an alternative to the class approach provided by Codea.

    It might be a good idea to pick out what specifically is different, so we can judge whether it is worth using a different approach. Much as I admire your achievement, I wouldn't want to switch unless there were compelling reasons.

  • Posts: 9

    @Ignatz: Good point. Well, I was just willing to share some piece of code of mine. :) From what I can see, here is the actual reference for Codea's built-in class. Well, I can outline a few more features that my implementation adds on the top of that. For more details, one can just take a look at the quicktour.

    First of all, it's really simple to get with. As-is, it can't be used straight with Codea, as there is no "require" function, but you can use this file instead:

    You create a class using the returned global function, named "class". The class constructor is named "__init" (which differs from the one used in Codea's classes implementation, "init").
    I'll use the same examples given on this page:


    -- An "Animal" class
    Animal = class {talk = ""}
    function Animal:__init(word)
    self.talk = word
    end
    function Animal:speak()
    print( self.talk )
    end
    anAnimal = Animal('I am an Animal!')
    anAnimal:speak() --> 'I am an Animal!'

    There is also support for single inheritance.

    -- A "Dog" class deriving from "Animal" class
    Dog = Animal:extends()
    function Dog:__init(word)
    self.talk= word
    end
    doggy = Dog('Woof')
    doggy:speak() --> 'Woof!'

    Classes that inherits can still use their superclass methods:

    -- Redefining "speak" method
    function Dog:speak(word)
    print(word..' '..word..' '..word)
    end
    doggy = Dog('Ima doggy!')
    doggy:speak('woof!') --> woof! woof! woof!
    -- "doggy" can still use Animal class "speak" method
    doggy.super.speak(doggy) --> Ima doggy!

    There is also some basic support for mixins, named classes.

  • IgnatzIgnatz Mod
    Posts: 5,396

    Again, without any wish to downplay what you've achieved...

    Codea also has inheritance, allowing the child class to use or override the parent code.

    I'm not sure what advantage named classes have. We tend to put big classes in their own tabs, which makes them easy to manage.

    Mixins sound interesting, but I can't think of an obvious application.

    But I'm just one voice, and certainly not an expert. There are others here much better qualified to comment.

Sign In or Register to comment.