luau icon indicating copy to clipboard operation
luau copied to clipboard

Inferred Table-Mutation & Lookup

Open MagmaBurnsV opened this issue 2 years ago • 1 comments

It is not uncommon for users to write wrappers for mutating keys inside an unsealed table to create custom services, for example:

local t = {}

local function add(k, v)
	t[k] = v
end

local function get(k)
	return t[k]
end

add("key", 1)
local data = get("key")

However, key doesn't get added to the type for t this way and thus indexing it will not return any type data. The same goes for the get function, it will not return any type data for its lookup.

It would be nice if t was inferred to contain key after add was called in this scenario. Granted, this wouldn't solve cross-modular indexing since technically the order of each element being added is not guaranteed, but this feature would be nice to have none-the-less.

MagmaBurnsV avatar Apr 03 '23 15:04 MagmaBurnsV

This would be pretty challenging to implement. We might be able to get to it someday, but not for awhile.

andyfriesen avatar Apr 06 '23 19:04 andyfriesen