lua-users home
lua-l archive

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


Brian Hagerty wrote:
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.

Sure it does. Just don't hide it. (See below).

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.

Yes, but I could easily write this:

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

And it would act *exactly* the same.

That sort of thing is often useful: you don't always want to call
a function with the table it lives in (sometimes you want to call
it with another object in the chain, for example.)

Finally, consider functions implemented in C. No "self" parameter there;
no names at all.