lua-users home
lua-l archive

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


On Mon, Jun 10, 2019 at 2:49 PM Dibyendu Majumdar <mobile@majumdar.org.uk> wrote:
I am not sure of the usefulness of the proposed <const> qualifier, but
it got me thinking whether it might be possible to implement compile
time named constants using similar annotations. That is to say the
code would be equivalent to writing directly using literals.

I certainly agree that compile-time named constants would be a nice thing to have, but I'm not 100% sure if it would have a measurable impact, because upvalues are also resolved at compile-time and you still have to push the value on the stack anyway. There's also not really a good way for modules to export compile-time named constants, which puts a significant limitation on how useful they are.

However, non-assignable variables are pretty useful. There are some compiler optimizations that can happen when you know a variable is read-only, and of course asserting that something is read-only can help avoid programming mistakes.

There's also no real reason the same qualifier couldn't be used for both. It would just be part of the optimization: if a compiler can statically determine the value of an _expression_, it can replace it with the result of that _expression_, and being read-only and initialized with a literal is a good way to ensure that the value can be statically determined.

/s/ Adam