[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Suggestion: syntax sugar for object methods inside table constructor
- From: Paul Ducklin <pducklin@...>
- Date: Tue, 1 Jun 2021 14:58:24 +0000
I think that’s a terrible idea, if only because of the visual and grammatical ambiguity it introduces.
Table constructors either imply a numeric key when initialising an array or specify the key when adding a non-array item.
When I see { function..., function... } my natural inclination is to see those as array items 1 and 2, which they would be in the current syntax.
But then I think that...
local func = function() ... end
...looks nicer than...
local function func() ... end
...anyway.
It’s a happy reminder that functions are just regular objects like everything else, and makes it obvious that the function() keyword produces an r-value and is thus an expression in its own right.
I don’t understand the “problem” you are trying to solve, or how it would make your code easier to understand at a glance than the regular { key1 = value1, key2 = value2 } syntax.
> On 28 May 2021, at 10:06, Egor Skriptunoff <egor.skriptunoff@gmail.com> wrote:
>
> I'd like to suggest to treat
> { function a() end }
> as equivalent of
> { a = function() end }
>
> Such sugar would be useful in metatable constructors and in OOP.
> It would not break old code: new syntax was a syntax error in previous Lua versions.