gpt4 book ai didi

lua - 为什么我在我的 lua 函数中看不到打印

转载 作者:行者123 更新时间:2023-12-04 14:13:37 25 4
gpt4 key购买 nike

我的问题有多个部分,但首先这是我的示例 lua 代码 (test.lua):

local traceback = debug.traceback

local inspect = require('inspect')

local foo = "function nop(); print('this is war'); return true; end"

local f = loadstring(foo)

local result = f()

print(result)

local status, val= xpcall(function () return f() end,debug.traceback)

print('status .. ' .. tostring(status))

print(val)

所以,

  1. 当我运行 local result = f() 时。我仍然看到 result 值为 nil

  2. 当函数 f 被执行时。为什么我看不到 print o/p。

最佳答案

您加载到 f 中的 foo 中的代码块不会返回任何值。

它定义了一个函数,仅此而已。

如果你想在运行时从 block 中返回函数,你需要将 return nop 添加到字符串的末尾。

关于lua - 为什么我在我的 lua 函数中看不到打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32272377/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com