Lua中的“负载"有什么作用? [英] what do 'load' do in Lua?

查看:39
本文介绍了Lua中的“负载"有什么作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图解决我在理解Lua脚本中的 load 函数时遇到的问题,但是此命令没有任何示例或指南.它在自己的Lua网站上说 https://www.lua.org/manual/5.2/manual.html#pdf-load 此:

I was Trying to sove my problem in understanding load function in Lua Scripts but there was not any Examples or guides for this command . it tells in his own Lua Website https://www.lua.org/manual/5.2/manual.html#pdf-load this :

加载(ld [,源[,模式[,env]]])

有人可以向我描述吗?

推荐答案

load 接收一个块,对其进行编译,然后作为可以调用以执行该块的函数返回.例如,以下代码将创建一个将两个数字在一起的函数:

load takes a chunk, compiles it, and returns as a function that can be called to execute the chunk. For example, the following will create a function that will add two numbers together:

local func, err = load("return function(a,b) return a+b end")
if func then
  local ok, add = pcall(func)
  if ok then
    print(add(2,3))
  else
    print("Execution error:", add)
  end
else
  print("Compilation error:", err)
end

这应该打印 5 .

这篇关于Lua中的“负载"有什么作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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