lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]



-----Original Message----- 
> From: "steve donovan" <steve.j.donovan@gmail.com> 
> To: "Lua mailing list" <lua-l@lists.lua.org> 
> Date: 01-05-2014 18:44 
> Subject: Re: Ideas about colon operator syntax (and a patch in the work) 
> 
> On Thu, May 1, 2014 at 6:24 PM, Thomas Jericke <tjericke@indel.ch> wrote:
> > That's already the case. The : operator is inconsistent and the use of
> > the colon operator is hard to explain as it is now. I often get calls from
> > customers who ask me: "When do I have to use the ':' operator?"
> 
> It does puzzle newcomers, but it's the price of "no magic".  I suspect
> Lua would be slower if every function call had to work out if it was a
> method ....  
I am not so sure if I would implement it this way. Just as a Gedankenexperiment,
if methods would be first class citizen of Lua it would mean that the function itself
would know that it is a method or a static function.

So if you write:
table = {}
function table:f ()
  ....
end

f would contain a internal reference to table. So local b = table.f would contain that 
reference as well and b() would call f with table as first parameter.

I don't think there Lua would be slower, but it would need more memory.
It is actually possible to achieve that using tables and defining the call metamethod:

table = {
 f = { t = table }
}
setmetatable(table.f, { __call = function(method, ...) myMethod(method.t, ...)})

table.f() -- OOP call without ":" hurray!

> Another point of view: OOP is not fundamental to Lua as
> it is with Ruby or Python; a "method" call is precisely something that
> uses colon-syntax, which is convenient sugar for "lookup up on object
> and call, passing object first".
> 
> > Maybe it's just because I dislike the colon operator as it is now in Lua that I don't
> > have scruple to mess around with it ;-)
> 
> Fair enough ;)  Sorry I keep calling it a 'proposal', maybe the word
> 'patch' is more neutral.  And we all have the right to fool with our
> copy of Lua ;)
> 
> steve d.
There is a good reason why I insist on it. If you make a proposal, everyone on the list  gets
their hind legs and bares their teeth. No reason for that, I do no-one harm that isn't mad
enough to install my patch (once I release it).
--
Thomas