Local Table Destructuring - #223
Conversation
|
|
Is this syntax reserved for future? |
|
Could you put some simpler examples for clarity, and include notes on how this works with array-likes, classes, and extern types on the RHS? |
No, its a function call. |
This is already valid syntax for a table call on a function named const const = function(t: { s: string })
print(t.s)
-- do smth with t
end
const { s = "meow" } -- this is a function calledit: jack hit post first and don't trust the syntax highlighter (it's wrong) |
What about a special character indicating destructuring? local @{ print, warn } = log
const @{ print, warn } = logor, if this is not optimal, restricting this destructuring syntax to local declarations (exclude constants) |
This isn't possible as outlined, at least not trivially. We could consider |
Got it. I've been brainstorming for some time and got two somewhat solutions.
First one is much simpler, but you lose the chance of having restrictions against rebinding your keys. And the second one is much more complex to implement, even with type annotations, |
you'll probably want to peek for |
Sorry for the very late reply. Basic examples: local { x, y } = Point
--// equivelent to
local _temp1 = Point
local x = _temp1.x
local y = _temp1.ylocal player, { health, mana } = GetPlayer(), GetStats()
--// equivelent to
local player = GetPlayer()
local _temp1= GetStats()
local health = _temp1.health
local mana = _temp1.manaArray-like --// not supported
local { 1, 2 } = list
--// not supported
local { first = 1 } = list
--// if users want array unpacking, they should continue using:
local first, second = list[1], list[2]
-- or table.unpackClasses --// NOTE: I havent used classes yet, so I can only give few examples
--// basic class instance destructuring is same as the basic example
--// assuming you have a class
class Point
public x: number
public y: number
function length(self)
end
function fromAxisLength(...)
end
end
--// there are three possibilities
--// option 1:
--// if "obj.field" is valid, then { field } is valid too.
--// option 2: only fields
--// only allow declared fields ("public x", "public y"), rejecting methods.
--// option 3: public members
--// allow both public fields and public methods, but reject private ones once private members exist.
--// this is probably what would naturally happen anyway if the language already prevents accessing private members via ----// obj.memberExtern types type Config = {
enabled: boolean,
timeout: number,
}
local config: Config = GetConfig()
--// the destructured variables inherit the field types
local { enabled, timeout } = config
-- enabled -- boolean
-- timeout -- number |
|
although for array destructuring, I might edit the RFC and propose that too with syntax similar to JS |
rendered
Note: I am fully aware of RFC 'Granural Imports', 'Table Destructing Syntax' proposals made by other people.
My RFC proposes simpler syntax, doesn't require new keywords, and is not limited to top-level scope with the usage of
require().