Here's a slightly different perspective. (The answer is still no.)
Part of the reason that <const>
works like it does is because (at least in the standard Lua implementation) it is evaluated purely at compile time. This is very simple to do -- local variables are lexically scoped, and the compiler needs to know about the variable declaration to do anything anyway -- for example it needs to know that it is in fact a local, and whether or not it is an upvalue.
The VM runtime knows nothing about <const>
. The only difference is if the compiler decided to use the information to make some optimization.
This paradigm doesn't work so well for a theoretical const table feature. We can ask if it is reasonable to expect the compiler to be able to determine whether a table is const (and if so, which fields, or whatever depending on how you implement this feature). In many cases this may be possible through complicated static analysis, but such analysis is probably much more complicated that anything the Lua compiler is currently doing, and you may start running into Halting Problem kinds of issues in more complex edge cases.
Or, you might invent a way to provide additional information to the compiler that helps it resolve this question. But, if the compiler easily knows which value is a const table, and in what way it is const, I think you have just reinvented strong typing. People have given Lua stronger typing before. It may be an interesting discussion, but it is somewhat of a philosophical departure.
If you don't do it at compile time, I guess you do it at runtime. This means, you would have some tag or metadata carried with the table object in memory and the VM would check this at runtime. As you know Lua has a flexible user-accessible system that allows this kind of feature -- metatables. Yes, it is true that your suggested syntax may be pretty convenient for your exact use case, but maybe it is not good to use the same syntax for two features that actually work quite differently from each other. (Imagine the question "Why did <const>
fail at compile/load time here, but crash at runtime there?") And I think creative use of the existing features and defining appropriate helper functions may be able to achieve some kind of compromise between syntax convenience, runtime cost, and readability.