lua-users home
lua-l archive

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

On Fri, Feb 3, 2012 at 10:02, Fabien Bourgeois <> wrote:
> Le 02/02/2012 20:23, Pierre-Yves Gérardy a écrit :
> I've refactored with local but I've used to try this solution before and
> I've had a problem with it. Stop me if I'm wrong :
> with `new_db:_dbpath(path, ext)`, self is passed as first parameter.
> However, in the context of `open` method, self is lstore, not the new_db
> object. Thus, methods will operate on the global lstore, am I wrong ?

Nope, the column notation is just syntactic sugar.

function ``a:b(...) end` becomes ``function a.b(self,...) end``.

``a:b()`` is translated to ``a.b(a)``. The only difference is that the
interpreter saves a variable lookup in the first case.

Try this if you want to see what happens behind the scenes:

obj = {c = function (self, ...) print( "method", self, ... )end}

a = setmetatable( {}, {__index = function (tbl,key)
  print( "index", tbl, key )
  return obj
a.b:c( "grapefruit" )
a.b.c( a.b, "pineapple" )

-- Pierre-Yves