format = function(&Return, Length, Format, ...)
Return = string.format(Format, ...);
Return = string.sub(Format, 0, Length);
return 1;
end
local Test;
format(Test, 12, "Hello world %s! This is a test.", "Hello World");
print(Test);
我希望这能印刷," Hello world!"没有它被函数返回但由参数返回。
答案 0 :(得分:6)
您可以执行类似
的操作local function Pointer()
return setmetatable({},{
__tostring = function(self) return self.value end
})
end
format = function(Return, Length, Format, ...)
Return.value = string.sub(Format, 0, Length)
return 1
end
local Test = Pointer()
format(Test, 12, "Hello world %s! This is a test.", "Hello World")
print(Test)
答案 1 :(得分:1)
在您的示例中,您没有访问Return
,只是设置它;你也没有使用返回值' 1'。所以:为什么不这样做:
format = function(Length, Format, ...)
local Return = string.format(Format, ...)
Return = string.sub(Format, 0, Length)
local status = 1 -- i'm guessing this is a status code of sorts
return Return, status
end
local Test, stat = format(12, "Hello world %s! This is a test.", "Hello World")
代码审核说明:
我建议将所有格式组件保持在一起,这样您就可以轻松扩展它:
local Test, stat = format("Hello world %s! This is a test.",
["Hello World", 12], ['Joe', 5])