what do 'load' do in Lua?

蓝咒 提交于 2020-12-07 08:26:59

问题


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 :

load (ld [, source [, mode [, env]]])

can someone describe it to me please ?


回答1:


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

This should print 5.



来源:https://stackoverflow.com/questions/48629129/what-do-load-do-in-lua

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!