Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot define a readonly global with mutable fields #90

Open
hramrach opened this issue May 3, 2023 · 7 comments
Open

Cannot define a readonly global with mutable fields #90

hramrach opened this issue May 3, 2023 · 7 comments

Comments

@hramrach
Copy link

hramrach commented May 3, 2023

Reading the documentation I see no option to declare a global readonly (so that it cannot be replaced) but making all its fields mutable.

Understandably this is not a great interface but it's a fact of life that a runtime provides its data to lua code in this fashion.

@alerque
Copy link
Member

alerque commented May 3, 2023

I don't think it is currently possible to lint for that, nor will I likely have time or interest in figuring it out. If you can figure out a clean way to implement it I'd consider a PR contributing a new config.

@arichard4
Copy link

arichard4 commented May 6, 2023

If there are a fixed set of fields that you'd expect to be mutable, you can explicitly mark them out as being mutable on a global read-only like so:

.luacheckrc

stds.mutable_readonly = {
	read_globals={
		x = {
			fields = {
				example_field_name = {
					read_only = false,

				}
			}
		}
	}
}
std = "min+mutable_readonly"

test.lua

x = 3
x.example_field_name = 1

luacheck test.lua
test.lua:1:1: setting read-only global variable x

(If the fields can be tables, you'll also want to add "other_fields = true")

This seems to only work for globals/read_globals defined in a std, not if you define them directly in the config file. I don't know why, and this isn't clearly documented; I found this by playing around with the luacheckrc file a bit. ¯\ (ツ) /¯

@hramrach
Copy link
Author

hramrach commented May 6, 2023

Yes, there is a fixed set of fields that are mutable: all possible fields.

@alerque
Copy link
Member

alerque commented May 7, 2023

That isn't what "fixed set" means. Is there a master list of all possible top level keys?

@hramrach
Copy link
Author

hramrach commented May 7, 2023

It's basically access to something like the Unix environment - C strings indexed by C strings.

@ale5000-git
Copy link

There is also the opposite problem, how make the global mutable but all its field readonly?
When I extend the string library, example:

function string:findsomething()
end

and then I have some global string:

string1
string2

even objects with string fields:
my_object.string3

Then the string itself should be mutable but the function findsomething (or the functions) not.

Note: This is just an example but pose the case I have 100 functions and 100 strings, then I have to manually specify 10000 cases to make the base object read-write but fields read-only?

@ale5000-git
Copy link

ale5000-git commented May 15, 2024

Why not something like this?

globals = {
	string1 = {
		read_only = false,
		read_only_fields = true,
		fields = {
			findsomething = {}
		},
	},
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

4 participants