相关疑难解决方法(0)

如何向表类型添加方法?

如何向表类型添加方法?我正在尝试编写一个搜索表值的方法.到目前为止我有.

function table:contains(value)
  for _, v in ipairs(self) do
    if v == value then return true end
  end
  return false
end
Run Code Online (Sandbox Code Playgroud)

然而,当我尝试做以下事情时.

t = {'four', 'five', 'six'}
t:contains('five')
Run Code Online (Sandbox Code Playgroud)

我收到了错误.

stdin:1: attempt to call method 'contains' (a nil value)
Run Code Online (Sandbox Code Playgroud)

有什么建议?

lua metaprogramming

7
推荐指数
2
解决办法
3844
查看次数

标签 统计

lua ×1

metaprogramming ×1