lua-users home
lua-l archive

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


gary ng wrote:
--- Brian Hagerty <Brian.Hagerty@LilypadNetworks.com>
wrote:
But since 'self' is a special local symbol in Lua,
Is that true ? I think it can be called foo or bar or
anything.


No, the self parameter in a colon-defined function can't be called foo or bar or anything else (as far as I understand). Here is the colon syntax for a OO-method definition (Blue Book pg 150):

function Account:deposit (v)
  self.balance = self.balance + v
end

In this case, balance is an instance variable previously defined in the Account table. self is the "hidden" parameter that refers to the calling object (Account, in this case).

Notice that the "hidden" parameter idiom of colon-functions does not provide another place for you to create a different name for that hidden parameter. In fact, 'self' was not explicitly defined anywhere in the function. You just use it per the implied Lua semantics. The semantics of the colon operator requires that the hidden parameter must be a local variable called self. -- In particular, this:

function Account:deposit (v)
  foo.balance = foo.balance + v
end

is not a way of using foo as the name of the hidden parameter. foo, in this case, is a reference to an outer variable, outside the lexical scope of the current function.

// Brian Hagerty