Posted 16 March 2013 - 03:05 PM
So, after a couple of hours figuring out how this could work I finally got it working.
If anyone of you ever programmed in Java: These functions should make it look like java oop :D/>.
So, here's the api:
Using this is pretty simple.
First you'll have to create an object. So create a function that has the same name as your object should have.
That function then has to contain all the functions and variables like this:
You can refer to any variable just the same as you refer to it in an api (It uses the same concept)
the function testObject in testObject is the constructor, it will be called if you create a new object, but I'll describe that a littlebit further down…
The this represents the enviroment. So this.name = name sets the index name in the object to name (You need that for usage like in this example when you would mix up with locals).
Befor you can use an object, you have to register it. Simly call the load function with the name of your object (as a string)
and it will register it (In this example it is "testObject"). I had some error messages in there to made debuging easier, but those functions need the rest of my api that's not finished yet…
But anyways, now we can create our new object by typing object = new("testObject", "Test"). The second parameter (and any following) is what it will be send to the constructor. If you don't have one (it's not neccessary) it will ignore that.
So, if we did everything correct, if we now type object.printName() it should print "Test" to the term.
Another part is extending. If you have two objects and one objects has to use the functions of the otherone, you can write anywhere in your object extends("yourOtherObject"), and it will inherit it's functions. You can address the constructor of the superobject by typing super(args), just as you would do in java. Note that one object can only extend one other object!
And finally, if you want to add a function to your object, use the abstract function and it will insert it. If you simply do object.func = func, as you would do with any other variable, other functions (in that object) won't be able to access it's enviroment, so you couldn't use print(name) in that case, so better use abstract :D/>.
(Only pass functions to abstract, otherwise it will crash! Any other variable can be inserted just fine the normal way. Like I said, had to remove error detection :D/>)
I hope there are no bugs in this api and that you have a use for it.
JokerRH
If anyone of you ever programmed in Java: These functions should make it look like java oop :D/>.
So, here's the api:
Spoiler
objects = {}
function load(name)
local func = getfenv(2)[name]
local env = {_class = name}
setfenv(func, setmetatable(env, {__index = _G}))
func()
if not objects[name] then objects[name] = {} end
objects[name].sub = func
end
function extends(super)
local name = getfenv(2)["_class"]
if objects[name] and objects[name]["super"] then return end
objects[name] = {}
objects[name]["super"] = objects[super].sub
objects[name].superName = super
end
function new(name, ...)
local env = setmetatable({_class = name}, {__index = _G})
local extends = objects[name]["super"]
if extends then
setfenv(extends, env)
extends()
env._super = objects[name].superName
env.super = env[env._super]
env[env._super] = nil
end
local new = objects[name]["sub"]
setfenv(new, env)
new()
env.this = env
if type(env[name]) == "function" then env[name](...) end
return env
end
function abstract(object, index, func)
object[index] = func
setfenv(object[index], object)
end
Using this is pretty simple.
First you'll have to create an object. So create a function that has the same name as your object should have.
That function then has to contain all the functions and variables like this:
function testObject()
function printName()
print(name)
end
function testObject(name)
this.name = name
end
end
You can refer to any variable just the same as you refer to it in an api (It uses the same concept)
the function testObject in testObject is the constructor, it will be called if you create a new object, but I'll describe that a littlebit further down…
The this represents the enviroment. So this.name = name sets the index name in the object to name (You need that for usage like in this example when you would mix up with locals).
Befor you can use an object, you have to register it. Simly call the load function with the name of your object (as a string)
and it will register it (In this example it is "testObject"). I had some error messages in there to made debuging easier, but those functions need the rest of my api that's not finished yet…
But anyways, now we can create our new object by typing object = new("testObject", "Test"). The second parameter (and any following) is what it will be send to the constructor. If you don't have one (it's not neccessary) it will ignore that.
So, if we did everything correct, if we now type object.printName() it should print "Test" to the term.
Another part is extending. If you have two objects and one objects has to use the functions of the otherone, you can write anywhere in your object extends("yourOtherObject"), and it will inherit it's functions. You can address the constructor of the superobject by typing super(args), just as you would do in java. Note that one object can only extend one other object!
And finally, if you want to add a function to your object, use the abstract function and it will insert it. If you simply do object.func = func, as you would do with any other variable, other functions (in that object) won't be able to access it's enviroment, so you couldn't use print(name) in that case, so better use abstract :D/>.
(Only pass functions to abstract, otherwise it will crash! Any other variable can be inserted just fine the normal way. Like I said, had to remove error detection :D/>)
I hope there are no bugs in this api and that you have a use for it.
JokerRH