lua-users home
lua-l archive

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


On Sun, Aug 28, 2011 at 05:20:13AM +0200, Should Pain wrote:
> I would just say one more setence.  

I would add three remarks about defining with the colon notation.

At the definition stage, you are allowed
    function a:fct(...)         
    -- automatically defines 'self' as first arg; if you refer to 'a'
    -- inside, it is the global 'a', not the first arg.
which is syntactic sugar for
    function a.fct(self,...)  
    -- 'self' is the first arg, if you refer to 'a' inside, it is the 
    -- global 'a', not the object.
not for 
    function a.fct(a,...)    
    -- 'self' is the global 'self' 

You are not allowed
    a:fct = function(...)
although
    a.fct = function(self,...)
is legal.

Finally, even if you defined a function via colon notation, e.g:
    a = {}
    function a:fct()
        print(self)
        end
there is nothing object-oriented about the function.

fx = a.fct
fx "Hello" --> Hello

Dirk