lua-users home
lua-l archive

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


I wrote a patch on 5.1.something a while ago to support :[ _expression_ ] as a way to invoke OP_SELF. As I recall, I had to patch both the parser (obviously) and OP_SELF though both changes were minor.

It definitely helps syntactically for cases where you want to do things like register callbacks to invoke — e.g., a weak table of object to selector mappings to be used for notification. It also allows one to have private methods by using hidden tables as keys.

I haven't kept my patch around because I have been trying to stick closer to stock Lua more of the time, but if I could make one syntactic change in Lua it probably would be this. (That probably gets followed with wanting obj:method and obj:[ _expression_ ] when used outside of a call context to be shorthand for creating an appropriate closure object, but that's a much more complicated patch and there are interesting questions about whether it should do the method lookup immediately or at call time.)

Mark

On Apr 19, 2012, at 7:10 AM, y s <y.s.outside@gmail.com> wrote:

Lua 5.1.4

For example:

bar = {} 
bar.name = 'test' 
bar['123.com'] = function(self) print(self.name) end 

I can't call the method like below:

bar:['123.com']() 
stdin:1: '<name>' expected near '[' 

Althought below works:

bar['123.com'](bar) 

But I this is somehow ugly. Is there a syntax sugar for this situation?

Or if it really cannot do it, will Lua team add this syntax in future?

Or Lua team did this intentionally?