Particularly when using modules, I often find myself wanting to access the contents of a table, where I don’t actually care about the table itself. For example:
local Fusion = require(ReplicatedStorage.Fusion)
local New = Fusion.New
local Children = Fusion.Children
local OnEvent = Fusion.OnEvent
local OnChange = Fusion.OnChange
There’s two problems with the above code;
- We’ve polluted the namespace with
Fusion
- a table we only need because we’re requiring a module. We pull out all the members we care about and store them separately for conciseness. - We’re taking up a lot of space with repetitive statements, that contain unnecessary duplication of the member names on each line.
This can also happen if you store your Luau types in a dedicated module - it gets especially awkward with generics, because they have to be re-specified:
local Types = require(Package.Types)
type StateOrValue<T> = Types.StateOrValue<T>
type Set<T> = Types.Set<T>
type Dependency<T> = Types.Dependency<T>
Therefore, I propose a general solution to this kind of problem - destructuring. This already exists in other languages like JavaScript; the idea is to allow multiple variables to be declared based on the contents of a table:
local tbl = {
foo = 2,
bar = true
}
local {foo, bar} = tbl
print(foo, bar) -- 2 true
This would simplify the above examples and eliminate a lot of duplication:
local {New, Children, OnEvent, OnChange} = require(ReplicatedStorage.Fusion)
type {StateOrValue, Set, Dependency} = require(Package.Types)
It remains to be seen whether some different syntax is preferable, or how well this deals with edge cases and backwards compatibility concerns.