5.4 constant in C API

70 views
Skip to first unread message

Stefano

unread,
Jun 9, 2025, 9:47:46 AMJun 9
to lua-l
Hello,
I have read about a new <const> keyword in 5.4 to define constants. I am
wondering how I could leverage this to create module-level constants in
the C API, as in:

int luaopen_mymod (lua_State *L)
{
lua_newlib (L, my_mod_functions);
lua_pushinteger (L, MY_C_ENUM_OR_DEFINE);
lua_setfield (L, "MY_CONST", -2);

return 1;
}

Is there a way to protect mymod.MY_CONST from overwrite using the new
feature?

Thanks,
Stefano

Matthew Wild

unread,
Jun 9, 2025, 10:01:14 AMJun 9
to lu...@googlegroups.com
No, <const> is for local variables, not table fields. What you are
looking for is perhaps read-only tables, which have been possible
since at least Lua 5.0: https://www.lua.org/pil/13.4.5.html

Regards,
Matthew
Reply all
Reply to author
Forward
0 new messages