Page 1 of 1
[Solved] Lua constant
Posted: Sun Jul 11, 2021 11:30 pm
by Kavaline
Searching about constants in lua, I found this link:
https://stackoverflow.com/questions/123 ... ob-with-it
Here was told that in lua 5.4, we can use constants without tricks. Is possile to add this to LÖVE in future?
Re: Lua constant
Posted: Mon Jul 12, 2021 3:27 am
by darkfrei
How about just not change vars?
Re: Lua constant
Posted: Mon Jul 12, 2021 10:49 am
by pgimeno
No, I don't think it will be added in future. But you can use for example
LuaPreprocess.
Re: Lua constant
Posted: Mon Jul 12, 2021 5:06 pm
by ReFreezed
Whether Lua 5.4 style constants will be supported in LÖVE in the future depends on if the feature gets added to LuaJIT. There are some post-Lua 5.1 features that have been added to LuaJIT (like 'goto') so who knows.
Re: Lua constant
Posted: Mon Jul 12, 2021 5:08 pm
by grump
For integer constants you can do this in LuaJIT:
Code: Select all
local ffi = require('ffi')
local MyEnum = ffi.typeof([[
struct {
enum {
CONST_A = 23,
CONST_B = 1337,
CONST_C = 42,
};
}
]])
print(MyEnum.CONST_B) -- 1337
MyEnum.CONST_A = 1 -- error: attempt to write to constant location
This works for integers only though; no other types are supported.
Re: Lua constant
Posted: Tue Jul 13, 2021 11:50 pm
by Kavaline
pgimeno wrote: ↑Mon Jul 12, 2021 10:49 am
No, I don't think it will be added in future. But you can use for example
LuaPreprocess.
This is really nice for other purposes!
ReFreezed wrote: ↑Mon Jul 12, 2021 5:06 pm
Whether Lua 5.4 style constants will be supported in LÖVE in the future depends on if the feature gets added to LuaJIT. There are some post-Lua 5.1 features that have been added to LuaJIT (like 'goto') so who knows.
Good to know this one, specially for debug. I saw some features from updated versions, due to this I suggest the constant too
grump wrote: ↑Mon Jul 12, 2021 5:08 pm
For integer constants you can do this in LuaJIT:
I'm developing a client-server, this will be useful, thanks! With strings have nothing like this?
Re: Lua constant
Posted: Thu Jul 15, 2021 9:42 pm
by RNavega
You can use the __newindex metamethod of a metatable to implement read-only (aka constant) tables that can have strings / int values etc, like this:
https://www.lua.org/pil/13.4.5.html
What makes them read-only is the fact that if you try to modify a value or add a key, they throw an error during runtime.