lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]




On Sat, Apr 24, 2021 at 7:56 PM 云风 Cloud Wu <cloudwu@gmail.com> wrote:
The type of constants in Lua can only be boolean, number, string now.
It looks good If it can support more types.

My suggestion is that if it assigns a global var to a const local var,
 the var is evaluated during the loading stage rather than running.

Idioms like :

local print <const> = print

function foobar()
   print "Hello World"
end

The local print can be a light C function constant. It can reduce the
memory footprint (fewer upvalues) and may faster. And it's more
friendly to the JIT or AOT compiler.



"local print <const> = print" cannot guarantee that the routine "print" is the standard routine, as another module can override it. I have replaced "print" with a userdata object that had a "__call" metamethod before to redirect "print" output away from the console. 

A table that's assigned to a 'const' variable can still be modified as well, only the variable referencing it can't be changed.

An example:

local T <const> = { 1,2,3 }
some_function(T)

There is no guarantee that T has not changed. I don't think the language will support your idea without substantial changes to the syntax and semantics.

--