Add a `const` keyword or something similiar to Luau

As a Roblox developer, it is currently annoying to have constants exist in your code without guaranteeing them as true constants. This is desirable because it adds some amount of security to code while writing it as well as potentially improving intellisense (a language server or Studio’s analysis could throw an error if a constant variable was overwritten, which is desirable behavior).

Most modern programming languages, including Javascript and Ruby, have some form of constants. Lua does not at the moment. The addition of a keyword to declare a constant would bring Lua more inline with the rest of the programming community and allow us to use the good practice of defining actual constants in our development.

Normally I would be hesitant to suggest a keyword be added since the answer is almost always “no”, but with the recent addition of continue as a context sensitive keyword, I feel comfortable suggesting keywords that can be context specific – such as const. In all cases, const variableName will currently produce a syntax error. Thus, it can simply be changed to create a constant and it won’t break any existing code.


This is something I’ve always wanted as well.

In most of my scripts, I have a dedicated category listed out with the constants in my script. I also use constant notation (all caps), but it always feels kind of awkward for these to not actually be constant.

I am more than open to the intellisense gains from this e.g. how Visual Studio displays its literal value in the tooltip.

My main concerns, and one of the gray areas of this suggestion, stems from how this might be handled for tables. If I create a ModuleScript and I want to define constants in this module, what’s the best way to do that?

Right now my only choice would be something like this:

local MyModule = {}
const MY_CONSTANT = 123
return MyModule

but this won’t throw an error if MY_CONSTANT is written to unless I use a metamethod for __newindex which is not a good idea in most cases where performance is desired. Lua is (to what I know) not designed to run reference types, so simply having MyModule.MY_CONSTANT point to MY_CONSTANT isn’t an immediate option, if I’m not mistaken.

This also hints at the implementation of something I’ve personally wanted which is something comparable to C#'s inline getter/setter system, but this is getting into extreme complexity and is something for another thread of its own. MyModule.MyReadOnlyValue { get; } = "Hello, world!"

Aside from this roadblock, I have huge approval for an idea like this.

1 Like

Repeating what I said in the switch case thread:

The continue keyword was a special case, because it has very minimal cost (almost no engineering decisions to make because there’s an obvious way it should work), is immediately and fully understood by anyone who has programmed in another language, and has a very positive impact on a lot of code.

Don’t expect other significant changes to the language syntax.


That’s fair enough, though there’s nothing wrong with requesting these things – I’ll be honest and say that I wasn’t expecting a constant keyword, but I try to make feature requests for things even if I don’t consider them likely.

Especially with the recent sign that keywords might get added if there’s a compelling enough reason, I feel more comfortable suggesting this than I would at any other time in Roblox’s history.

1 Like