lua-users home
lua-l archive

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


On 04/19/2012 10:10 AM, y s wrote:
> Lua 5.1.4
> 
> For example:
> 
>     |bar = {} 
>     bar.name = 'test' 
>     bar['123.com <http://123.com>'] = function(self) print(self.name) end 
>     |
> 
> I can't call the method like below:
> 
>     |bar:['123.com <http://123.com>']() 
>     stdin:1: '<name>' expected near '[' 
>     |

Maybe this solution might help.

  bar = {}
  bar.name = 'test'
  bar['123.com <http://123.com>'] = function(self) print(self.name) end

  -- Make bar behave like a function
  setmetatable(bar,{__call=function(t, k, ...) return t[k](t,...) end})

  bar('123.com <http://123.com>')