[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: table:method as an expression (I know I know, just hear me out, okay? :D)
- From: DarkWiiPlayer <darkwiiplayer@...>
- Date: Sun, 22 Mar 2020 10:55:38 +0100
Greetings lua-l
No, I'm not suggesting some_table:some_method should create a closure,
if that's what you were expecting ;)
I noticed the other day that I surprisingly often find myself writing
code like this:
function_with_callback(stack.pop, stack, some, other, arguments)
or (not as often, but it still happens)
for item in stack.pop, stack do
print(item)
end
As I was once again writing the same snippet of code, I had the
following idea:
What if I could just write
function_with_callback(stack:pop, some, other, arguments)
for item in stack:pop do print(item) end
Unlike the more common (as far as I can tell) suggestion that this
expression should generate a closure, this approach doesn't hide slow
operations (aka. function closing) behind an innocent-looking syntax
feature.
As seen above, it also fits neatly with two common patterns:
- function+state, as seen in the example with the for loop
- callback+argumetns, as seen in the other example
The only downside I can see is the added complexity, but:
- It shouldn't be much added complexity in the parser, though I might be
mistaken here (I'm not very familiar with Luas C code yet)
- It doesn't seem hard to understand for people learning the language,
as it's very similar to the already existing some_table:some_method()
syntax
That's my thoughts at least, I'll be looking forward to reading what all
of you think :)
PD: With corona sadly still being a thing, I'll take this chance to say
I hope all of you stay safe and well :)
Attachment:
signature.asc
Description: OpenPGP digital signature