lua-users home
lua-l archive

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


I explored this idea from the C++ side a few months back:

http://lua-users.org/lists/lua-l/2006-07/msg00100.html


The wise and patient Lua community gently convinced me to stick with the
colon syntax  :-)

http://lua-users.org/lists/lua-l/2006-07/msg00101.html
http://lua-users.org/lists/lua-l/2006-07/msg00113.html

-Erik

> -----Original Message-----
> From: lua-bounces@bazar2.conectiva.com.br [mailto:lua-
> bounces@bazar2.conectiva.com.br] On Behalf Of Brian Hagerty
> Sent: Thursday, March 15, 2007 12:10 AM
> To: Lua list
> Subject: Colon Operator: Superfluous Syntax?
> 
> Do we really need the colon operator to implement OO-like method
> calls?  Perhaps Lua's syntax could be simplified by defining a
> small extension to the semantics (and implementation) of self in
> function / method definitions and obviating the need for the
> colon syntax.
> 
> Thus, the object.method( object, arg) "extra argument" calling
> idiom is just a formal semantic equivalence for the call to
> object:method( arg).  But why couldn't Lua recognize a
> colon-free definition & calling syntax of 'self' methods like
> below (showing a table constructor & function constructor for
> the withdraw and deposit methods, adapted from pg 150 of the
> Blue Book):
> 
> Account = {
>    balance = 0,
>    withdraw = function (v)  -- note: no explicit 'self' arg
>      self.balance = self.balance - v  -- self is inferred
>    end
> }
> 
> -- and --
> 
> function Account.deposit (v)  -- note: no colon operator
>    self.balance = self.balance + v  -- self is inferred
> end
> 
> 
> -- with corresponding OO-like method calls: --
> 
> Account.deposit( 200.00)   -- no extra 'Account' arg needed
> Account.withdraw( 100.00)  -- and no need for colon syntax
> 
> 
> Notice that I've excluded 'self' as an explicit parameter in the
> definition of withdraw, and I've used a dot instead of a colon
> in the definition of deposit.  This would be a syntax error
> and/or runtime error under the current definition of the
> language since the function bodies make references to self.
> 
> If there is no colon, is there no self? -- sounds like a
> question for Descartes: cogito ergo colon? :-)  Under the
> current language definition, the answer is yes: no colon, no self.
> 
> ...
> 
> But since 'self' is a special local symbol in Lua, couldn't the
> caller / object identity of the reference to self be inferred
> without the need for either an explicit self parameter (in the
> table constructor case) or a colon operator (in the function
> constructor case)?
> 
> That is, in both the definition of functions/methods and the
> calling of functions/methods, shouldn't a local reference to
> 'self' automatically imply a reference to the caller (receiving
> object)?  The semantics of 'self' implies a reference to the
> caller even without a self parameter (in the explicit idiom) or
> colon operator (in the implicit idiom).
> 
> I think the answer boils down to a runtime implementation issue.
>   If the colon operator is implemented by pushing a "hidden"
> reference to the caller onto the *argument* stack, then it would
> be messy to sort through argument references on the stack (i.e.
> use arg[i] in the explicit self case or arg[i+1] in the implicit
> self case).
> 
> But the self reference to the caller needn't be pushed on to the
> *argument* stack -- that is, self shouldn't be treated as a
> calling argument (or definition parameter) at all!  That's just
> a cute way of formally mapping OO-method semantics into
> traditional functional syntax.  Instead, during a function /
> method call, the caller's reference is always available nearby
> anyway, usually elsewhere in the call frame; hence no need for
> "hiding" it in the argument stack.
> 
> ...
> 
> Implementation details aside, it seems to me that from the
> semantics of any local reference to self in a function body, Lua
> should always be able to infer the caller / object from the
> lexical scope -- without the need for a colon operator or
> "hidden" parameters.
> 
> Thus a "function" definition differs from a "method" definition
> *only* because the function makes no reference to a local self
> variable, whereas the method does make reference to a self
> variable.  Both "functions" and "methods" should be defined /
> called with the same dot operator and the same parameter /
> argument list syntax.  Only "proper" parameters / arguments
> should appear in definitions / calls -- no need for "hidden"
> self parameters or "extra" self arguments.
> 
> // Brian Hagerty