[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Suggestion: syntax sugar for object methods inside table constructor
- From: DarkWiiPlayer <darkwiiplayer@...>
- Date: Fri, 28 May 2021 12:55:54 +0200
Generally speaking, I agree that this would be useful. I've gotten
mildly annoyed more than once about the inconsistent syntax when
defining functions on an existing table vs. defining them in the table
constructor, to the point where I often actually construct an empty
table, then start defining functions using the `function tab.key(args)`
syntax.
That being said, this still leaves the problem of methods, where one
would still have to explicitly list the `self` parameter, so methods
defined in table constructors still wouldn't look the same as methods
defined afterwards. There might be another simple syntax change to
address this as well, but all I could think of is adding a colon like
`local tab = { function :method(args) }` and that looks very ugly. Maybe
someone can come up with a better solution to this?
On 28/05/2021 11:05, Egor Skriptunoff 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.