[RFC] Local function call

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

[RFC] Local function call

Jerome Vuarand
Hi list,

I had an idea this morning, and it's worth a mail to the Lua mailing
list. Please read to the end, the idea comes at the end.

I was thinking of yet another object oriented implementation that
would roughly look like that (it's a bit long, but nothing really
interesting for now, keep reading) :

-- Yet another class implementation
function class(classname)
        return function(members) -- this is for parenthesis-less argument chaining
                local caller_env = getfenv(2) -- we will declare the class in
callers environment
                caller_env[classname] = {
                        new = function()
                                local object = {} -- we copy data members as is
                                local methods = {} -- we put methods in object metatable's __index table
                                for k,v in pairs(members) do
                                        if type(v)=="function" then
                                                methods[k] = v
                                        else
                                                object[k] = v
                                        end
                                end
                                setmetatable(object, {__index=methods})
                                return object
                        end;
                }
        end
end

-- Let's create a class
class "CFoo"
{
        a = 1;
        b = 2;
        c = nil;
        amethod = function(self)
                self.c = self.a + self.b;
        end;
}

-- Let's instantiate an object
local obj = CFoo.new()
print(obj.c)
obj:amethod()
print(obj.c)
----

Ok, tested and that works. A big drawback is that I can't declare
local classes, because getfenv returns only global environments. And
here come the idea. I thought it would be nice to be able to write :
-- Let's create a local class
local class "CFoo"
{
        a = 1;
        b = 2;
        c = nil;
        amethod = function(self)
                self.c = self.a + self.b;
        end;
}
----
The Lua compiler complains because there is no = signe before "CFoo".
It means that it's a possible future additionnal valid situation in
the Lua language. And what means the above block ? It can be
simplified as :
local f()
It's what I call a local function call (the call is local, not the
function). And the only difference with the normal function call, is
that inside the function, getfenv(2) returns a table of the local
variables of the caller chunk (with eventually the real environment
accessible in metatable __index of that table). That way the exact
same implementation of my class function above would do what I want.

I think it would be a nice addition to the future of Lua. I have no
idea of the possible implementation difficulties that exist in current
Lua implementation, but the principle is rather simple. What do you
think of it ?

Jérôme.
Reply | Threaded
Open this post in threaded view
|

Re: [RFC] Local function call

Javier Guerra Giraldez
On Sunday 28 May 2006 12:51 pm, Jérôme VUARAND wrote:
> It's what I call a local function call (the call is local, not the
> function). And the only difference with the normal function call, is
> that inside the function, getfenv(2) returns a table of the local
> variables of the caller chunk (with eventually the real environment
> accessible in metatable __index of that table). That way the exact
> same implementation of my class function above would do what I want.

i've also wanted to see some way to 'inherit' local variables (for other
purposes, not a class system); but using a table (even an environment table)
wouldn't be optimal.  remember that local variables aren't in a table (unlike
global variables), in the compiled bytecode local references are replaced by
namless 'slots'.  

i imagine there could be two approaches to implementing your 'local call':

A: preserving the variable names, and creating a table and populating it at
call time.

B: get rid of the whole 'slot' mechanism and replace it with lua tables, using
the variable names as keys, just like global variables

either way, it's a big space and time bloat to all programs.  bit step
backwards just to get one 'nice' feature

of course, i'd love if somebody (mike?) could optimize that so much to make it
worth.

--
Javier

attachment0 (205 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: [RFC] Local function call

Mildred Ki'Lya
In reply to this post by Jerome Vuarand

Why don't you use a syntax like that ?

foo = class {
        -- class definition
}

and

local bar = class{
        -- class definition
}

--
Mildred       <xmpp:[hidden email]> <http://mildred632.free.fr/>
Clef GPG :    <hkp://pgp.mit.edu> ou <http://mildred632.free.fr/gpg_key>
Fingerprint : 197C A7E6 645B 4299 6D37 684B 6F9D A8D6 [9A7D 2E2B]
Reply | Threaded
Open this post in threaded view
|

RE: [RFC] Local function call

Jerome Vuarand-2
In reply to this post by Jerome Vuarand
It was just an example to illustrate the concept. The principle was just to extend lua syntax a little to allow even more different semantics that those you can implement today.

-----Message d'origine-----
De : [hidden email] [mailto:[hidden email]] De la part de Mildred
Envoyé : 30 mai 2006 13:06
À : [hidden email]
Objet : Re: [RFC] Local function call


Why don't you use a syntax like that ?

foo = class {
        -- class definition
}

and

local bar = class{
        -- class definition
}

--
Mildred       <xmpp:[hidden email]> <http://mildred632.free.fr/>
Clef GPG :    <hkp://pgp.mit.edu> ou <http://mildred632.free.fr/gpg_key>
Fingerprint : 197C A7E6 645B 4299 6D37 684B 6F9D A8D6 [9A7D 2E2B]