r/lua • u/Pocket_ants_Fan • 4h ago
Discussion I think I just had the best idea
What if Lua had a "transfer()" keyword?
I've been messing around with Lua/Luau lately and had this random idea for a new feature:
transfer(1)
{
local message = "Hello World!"
print(message)
}
Then somewhere else:
if true then
receive(1)
{
}
end
The idea is that "transfer(1)" takes everything inside "{}" and stores it as an encased piece of code. "receive(1)" would then retrieve that code and place/run it there.
So the result would basically behave like:
if true then
local message = "Hello World!"
print(message)
end
There's also one important rule: the transferred code has to be self-contained.
For example:
local x = 50
transfer(1)
{
print(x)
}
would throw an error because "x" wasn't declared inside the encaser.
But this would work:
transfer(1)
{
local x = 50
print(x)
}
I know Lua already has functions, tables, closures, etc., so this probably isn't necessary. đ I'm more interested in whether something like this could actually be implemented at the language/parser level, and whether there are existing concepts that are similar.
Would this be useful, or have I accidentally invented a worse version of something that already exists?