Proposal: Allow `function name() ... end` inside of tables.

111 views
Skip to first unread message

Jonne Ransijn

unread,
May 20, 2025, 10:19:23 AMMay 20
to lua-l
This would have the same effect as `name = function() ... end`, just
like it does in global scope, and would allow for slightly more readable
OOP syntax:

```lua
Point = class {
function __new(self, x, y)
self.x = x or 0
self.y = y or x or 0
end,

function origin()
return Point(0, 0)
end,

function __add(self, other)
if type(other) == 'number' then
return Point(self.x + other, self.y + other)
end
return Point(self.x + other.x, self.y + other.y)
end,

function __tostring(self)
return string.format("Point(%d,%d)", self.x, self.y)
end,
}

print(Point.origin() + Point(3, 4))
```

I have attached a patch.
named-functions.patch

blog...@gmail.com

unread,
May 21, 2025, 4:12:14 PMMay 21
to lua-l
Looks nice but it hard incompatibility  + no way for create backward compatibility code :(
It looks cool, but I think the downsides of the consequences will outweigh the upsides. Everything has its price, and here the price will be high, and in return we will just get another record of what already exists. I am just expressing my opinion. But I appreciate Lua for the fact that every decision is weighed on a paranoid level. Increment and decrement are also easy to add, but I would rather write a = a + 1 in an idiomatic style than maintain another code base. I personally use changes to the language for my personal needs, but I do not think that these changes are worth adding to the main language, the beauty is that it is easy to add something, and also easy to statically link and not know about compatibility issues Once again, it looks interesting, but the price for it is too high. If we make such changes we will turn Lua into pluto (another fork of Lua with a bunch of syntactic changes) I like the way it looks, but I am against adding such for lua5.x.x, because I like the way it looks without it. Let me clarify again, this is just my opinion. I am very sensitive and excited about the language as a lua user :)
вторник, 20 мая 2025 г. в 17:19:23 UTC+3, Jonne Ransijn:
Reply all
Reply to author
Forward
0 new messages