lua-users home
lua-l archive

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

Consider using an upvalue to hand you the "self" parameter.

For instance instead of...

MyMethods = {}
MyMethods:Func1 = function() do return self.x*self.y end
MyMethods:Func2 = function() do return self.x+self.y end

NewInst = {x=1, y=2, Func1 = MyMethods.Func1, Func2 = MyMethods.Func2}

Result = NewInst:Func1()
-- or, similarly
Result = NewInst.Func1(NewInst)


CreateInstance = function() do
   local self = {x=1, y=2}
   self.Func1 = function() do return self.x*self.y end
   self.Func2 = function() do return self.x+self.y end

   return self

NewInst = CreateInstance()

Result = NewInst.Func1()


Of course arranging this via the C API is a bit more drawn out, but not
terribly difficult.
Also, I left out the part about double checking the parameter list if you
wish to allow the use of a :, but if you are defining the syntax for your
embedded script users rather than just telling them that its Lua then that
isn't even an issue.

-----Original Message-----
[]On Behalf Of M vd Honing
Sent: Sunday, May 30, 2004 8:26 AM
Subject: . vs :

Is it possible to automaticaly translate a . To : for function calling?
I know they do something different, but i do not like the use of two
different chars for what for the end user apears to be the same thing. I
cannot explain to them why setting a var requires a . and calling a function
requires a :
It is easier for them if they can use a . in both cases. See below:


Is the same as calling


Is there a way to tell lua to make it possible to use:


And still do: