<const> everywhere

116 views
Skip to first unread message

Martin Eden

unread,
Apr 15, 2026, 12:44:33 AMApr 15
to lu...@googlegroups.com
Hello guys,

I'm writing new code in Lua 5.5 and trying that new "<const>"
attribute for declarations. In short

  local <const> a, b = 1, 2

blocks writing to "a" and "b".

And

  local a <const>, b = 1, 2

blocks writing to "a".


That "<const>" is spreading just like "local":

  local <const> MyModernFunction = function() end

  local <const> MyModernTable = {}

For my case more than half use cases can be marked "<const>".


Maybe we can have "<var>" to mark write-enabled locations and
make "<const>" default?

(Like my wish to have "local" attribute as default.)

-- Martin

Sewbacca

unread,
Apr 15, 2026, 1:58:58 AMApr 15
to lu...@googlegroups.com
AFAIK <const> had been already introduced in 5.4.

I would much rather prefer const as a stand alone keyword that would have the same effect as "local <const>"

However making "local" const per default would break backwards compatibility in a huge way. Writing code that targets all Lua versions would move from hard to impossible (and because I am mainly writing code that targets all Lua versions take my paragraph above with a grain of salt).

I have the same argument against making "local" default.

In addition to that argument, making local the default does hide the declaration location in an unobvious way

take this function for example:

local function foo()
    bar = 10
end

foo()
print(bar)

What should be printed now? 10? nil?
If you ask any Lua version you'll get 10.
If local is the default then nil should be printed.

Let's say above "foo()" "local bar = 20" would have been declared. Should 10 or 20 be printed? All Lua versions agree on 10, a sane default implementation would agree. But from the above code example it is no longer obvious in that hypothetical implementation.

Let's say somewhere else (either in this file or another) "_G.bar = 20" would had been declared

Now it gets really complicated. Again every current Lua version agrees that 10 should be printed. In this hypothetical local default example however it would depend on if the parser sees this global declaration or if it doesn't.

All these questions suggest to me that a declaration is fundamentally a different action than an initialization. Having syntactic sugar to do both in one go is nice. Hiding that something is being assigned vs. declared however seems dangerous to me.

~ Sewbacca

Martin Eden

unread,
Apr 15, 2026, 2:48:15 AMApr 15
to lu...@googlegroups.com
On 2026-04-15 07:58, 'Sewbacca' via lua-l wrote:
> All these questions suggest to me that a declaration is fundamentally a different action than an initialization.

Many thanks for example, you convinced me that having "local" or
"global" is not the same as having nothing!

-- Martin

Eduard Qwerty

unread,
Apr 23, 2026, 6:10:02 AM (10 days ago) Apr 23
to lua-l

I think the best option is to add "const" keyword which means "local <const>". (But that can be confusing, and... it turns out, you can add a second tag using "const <tag>"? Sigh.)
среда, 15 апреля 2026 г. в 09:44:33 UTC+5, Martin Eden:
Reply all
Reply to author
Forward
0 new messages