guilin 桂林
guilin 桂林

Reputation: 17422

how to call lua method by method name

there is an object client which has many method, methods could be call like client:hget(key, field) or client:exists(key) etc.

Now, I need implement a function client_holder.call(cmd, ...)

local client = client_instance;
function call(cmd, ...)
    client[cmd](client, ...) // will this work??
end

Upvotes: 1

Views: 139

Answers (1)

hugomg
hugomg

Reputation: 69924

Yes, that should work. As the Lua manual states, the colon notation is just syntactic sugar:

The form

functioncall ::= prefixexp ‘:’ Name args

can be used to call "methods". A call v:name(args) is syntactic sugar for v.name(v,args), except that v is evaluated only once.

Upvotes: 2

Related Questions