On Mon, 3 Nov 2025, at 11:54, 'Sainan' via lua-l wrote:
> You declared a local called 'const' and a global called 'noop'. Neither
> of which are, in fact, constants.
dang, something looked a bit fishy already to me (haven't used const at all yet), then assumed as it compiled it was correct. Silly me. Thanks for pointing it out.
I explicitly do not want to modify Lua sources, stock Lua intended.
So here is the proper code example:
local noop <const> = function() end
local function main()
noop()
print("done")
end
main()
And the proper full example:
local noop <const> = function() end
local function generate_validator(spec)
if not os.getenv("LUA_ASSERT_ARGS") then
-- validation disabled, so "be light"
return noop
else
-- generate a full blown parameter validator function from the specs
return require("validator").generate(spec)
end
end
local main do
local main_validator <const> = generate_validator {
-- extensive description goes here (something like a JSON schema),
-- becomes a constant no-op if LUA_ASSERT_ARGS is undefined
}
function main(param1, param2, param3)
main_validator(param1, param2, param3) -- if a const no-op, ideally no byte code generated
print("done")
end
end
main(1, 2, 3)
> You meant to use 'local noop <const>', but even so, Lua does not have
> an optimising compiler as it would be a needless amount of added
> complexity.
I recently read somewhere (on this list probably that const has no runtime effects, as it is checked during compilation, not runtime. That triggered me into the above, trying to see if this was also checked at compile time.
Whether it is a "needless amount of added complexity" is an opinion. To me it is just moving cost from runtime to compile time.
This is the use case I had in mind, I would be curious to know if others see other uses for something like this.
Thijs