lua-users home
lua-l archive

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


On this day of 03/15/2007 12:47 AM, Brian Hagerty saw fit to scribe:
> gary ng wrote:
>> --- Brian Hagerty <Brian.Hagerty@LilypadNetworks.com>
>> wrote:
>> 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).

Yes, but you could also write:

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


I actually kind of like the colon because it makes it clear to me when
I'm calling a method and when I'm using a namespace.

I agree that you could conceivably detect the use of 'self' in a
function definition and deduce that there is a hidden parameter, thereby
obviating the need for the colon in the function definition. In this
sense, any function that is deduced to be a "method" (i.e., having a
hidden parameter) would have its prototype modified to have the hidden
argument added in front.

However I'm not sure how this would work for calling methods. If I were
to write,

a.deposit(3)

How does Lua know that deposit is a "method" and therefore the call
should be translated to a.deposit(a,3)? That would entail tables storing
not only the functions, but also extra data about the functions, like
"this one is a method", "this one isn't". And it would also entail
checking that meta-datum every time you call a function.

The nice thing about the current implementation is that, as I understand
it, the processing only happens at compile time, and from that point
onwards Lua is essentially ignorant of the fact that the function is
actually a method.

It seems like a lot of added complexity to me for relatively little
marginal benefit.

Cheers,
- David

-- 
~David-Haley
http://david.the-haleys.org