oop - Regarding colon operator in Lua -


why piece of code fail (attempt call method 'sort' (a nil value))

th> xyz = {1,2,3}                                                                       th> xyz:sort() 

while works

th> table.sort(xyz) 

because table table, contains generic functions manipulating tables provided standard library, isn't in metatable table default. in fact, table doesn't have metatable unless specified explicitly.

you manually though:

local xyz = {1,2,3} local mt = { __index = table} setmetatable(xyz, mt) xyz:insert(2) xyz:sort() 

Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -