Skip to content

partially implement type:setreadindexer#2549

Open
nnullcolumn wants to merge 3 commits into
luau-lang:masterfrom
nnullcolumn:setreadindexer
Open

partially implement type:setreadindexer#2549
nnullcolumn wants to merge 3 commits into
luau-lang:masterfrom
nnullcolumn:setreadindexer

Conversation

@nnullcolumn

@nnullcolumn nnullcolumn commented Jul 13, 2026

Copy link
Copy Markdown

previously, setreadindexer would error with type.setreadindexer: luau does not yet support separate read/write types for indexers.

now that read-only indexers are implemented, this PR suggests a partial implementation locked behind a new flag LuauUdtfTypeSetReadIndexerMethod and an old one LuauTypeFunctionTableIndexerIsReadOnly for serialization/deserialization. It will error if the table already has a read+write indexer, but otherwise allows the operation. Although partial implementation isn't ideal, some trivially-fixed-once-separate-read-write-indexers-are-implemented dragons are okay as this gets behavior into users' hands sooner and UDTFs are inherently forgiving towards mistakes. As usual, this PR / implementation (although small) is 100% human authored.

edit: the existing workaround people are likely to use, provided LuauTypeFunctionTableIndexerIsReadOnly is enabled:

type tbl_with_read_only_indexer<key, value> = { read [key]: value }

type function setreadindexer(tbl: type, index: type, value: type): type
	const new: type = tbl_with_read_only_indexer(index, value)
	const mt = tbl:metatable()
	if mt then
		new:setmetatable(mt)
	end
	for key, prop in tbl:properties() do
		const read, write = prop.read, prop.write
		if read and write and read == write then
			new:setproperty(key, read)
		else
			if read then
				new:setreadproperty(key, read)
			end
			if write then
				new:setwriteproperty(key, write)
			end
		end
	end
	return new
end

@nnullcolumn

Copy link
Copy Markdown
Author

whoops broke my own tests by changing an err msg :p

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant