Lua如何创建可用于变量的自定义函数? [英] Lua How to create custom function that can be used on variables?

查看:119
本文介绍了Lua如何创建可用于变量的自定义函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用像io.close()这样的方法,你可以像这样使用它:

  file:close()

有没有一种方法可以创建一个类似于此的自定义函数,可以在变量上调用它?



对于我来说,我试图用它通过使用string.find来查找空格来分离文本文件中的参数。



因此,在文本文件中,它看起来像

 这是一些输入

readArgs()函数应该返回表中的整个行,其中args [1] =So,args [2] =in ,args [3] =the等被调用后行

 函数readFile(file)
本地行= {}
断言(io.open(文件),文件无效或缺失)
local f = io.open(file)
for line in lines:做
行[#行+ 1] =行
结束
返回行
结束

函数readArgs(行) - 这是函数。最好在字符串行上调用它
- 这里的一些代码
结束


解决方案

根据您的描述,您听起来像是在类似于以下语法的内容之后:

  local line = readFile(file)
lines:readArgs(1) - 解析第一行{this,is,some,input}

Metatables可以解决这个问题:

  local mt = {__index = {}} 
函数mt .__ index.readArgs(self,linenum)
if self [linenum]然后返回nil结束

local args = {}
for each in self [linenum]:gmatch[^] +do
table.insert(args,each)
end
return args
end

您必须对 readFile 并将该metatable附加到您要返回的

 函数readFile(文件)
- ...
返回setmetatable(行s,mt)
结束

编辑:回答OP的评论,像这样的调用:

pre $ lines:readArgs(1)

仅仅是语法糖:

  lines.readArgs (行,1)

当lua虚拟机执行上述行时,会发生以下情况:




  • 是否有 readArgs key?

  • 如果是,则照常使用其相应的值作为语句的其余部分。 c>行有一个metatable .__索引?在这种情况下,它会这样做,因此使用分配给 __ index.readArgs 的函数。
  • readArgs
  • code>现在用上面的参数调用: self = lines, linenum = 1


这里没有什么特别的 self ,它只是一个常规参数;你可以将它命名为任何你想要的东西。


With methods like io.close(), you can use it like this:

file:close()

Is there a way to create a custom function that works like that, where you can call it on a variable?

For me I am trying to use it to separate arguments from a text file by using string.find to find spaces

So in the text file it looks like

this is some input

And the readArgs() function should return the entire line in a table with args[1] = "So", args[2] = "in", args[3] = "the" etc. after being called on the line

function readFile(file)
    local lines = {}
    assert(io.open(file), "Invalid or missing file")
    local f = io.open(file)
    for line in f:lines() do
        lines[#lines+1] = line
    end
    return lines
end

function readArgs(line) -- This is the function. Preferably call it on the string line
    --Some code here
end

解决方案

Based on your description it sounds like you're after something similar to this syntax:

local lines = readFile(file)
lines:readArgs(1) -- parse first line {"this", "is", "some", "input"}

Metatables can help with this:

local mt = { __index = {} }
function mt.__index.readArgs(self, linenum)
  if not self[linenum] then return nil end

  local args = {}
  for each in self[linenum]:gmatch "[^ ]+" do
    table.insert(args, each)
  end
  return args
end

You'll have to do a slight change in your readFile and attach that metatable to the lines you're returning:

function readFile(file)
  -- ...
  return setmetatable(lines, mt)
end

Edit: To answer the OP's comment, a call like this:

lines:readArgs(1)

is just syntactic sugar for:

lines.readArgs(lines, 1)

When the lua VM executes the above line the following happens:

  • Does the table lines have a readArgs key?
  • If yes then use its corresponding value as usual for the remaining part of the statement.
  • If no, does lines have a metatable.__index? In this case it does, so the function assigned to __index.readArgs is used.
  • readArgs is now called with the parameters above: self = lines, linenum = 1

There is nothing special about self here, it's just a regular parameter; you can name it anything you want really.

这篇关于Lua如何创建可用于变量的自定义函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆