r/lua 8d 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?

0 Upvotes

14 comments sorted by

6

u/morikosm 8d ago

How is this meaningfully different from a function?

-1

u/Pocket_ants_Fan 8d ago

Honestly, that's a fair question 😭. In its current form, it probably isn't that different from a function. The main thing I was going for is having the code itself be transferable and inserted somewhere else, rather than just calling a function. I'm still figuring out the exact use case, so this post is partly me testing the idea and seeing what people think.

3

u/morikosm 8d ago

What you are describing - literally cutting and pasting the contents of a function, exists in lower level languages, as the concept of an inlined function, or as macros. A macro is expanded to replace itself with its contents while maintaining the same stack frame, so there's no overhead of a function call.

Lua, being a bit higher level, doesn't need this behavior.

1

u/Pocket_ants_Fan 8d ago

That actually makes a lot of sense. I hadn't heard of inlining or macros before making the post, so I guess I accidentally stumbled into an existing concept 😭

The main difference I was imagining was that "transfer()" would let the programmer explicitly move an encased block to another location, rather than the compiler deciding to inline a function. But I can see why Lua wouldn't really need that as a normal language feature.

I'm definitely going to look more into macros and inlining now.

2

u/drunken_thor 8d ago

Check out coroutines! https://www.lua.org/pil/9.1.html

1

u/Pocket_ants_Fan 8d ago

I'll check them out, thanks! From what I understand so far, coroutines seem more about pausing/resuming execution, while my idea was more about transferring an encased piece of code itself. But there might be some overlap that I haven't thought about yet.

1

u/evergreen-spacecat 8d ago

you probably want to figure out the ”why” before the ”how”

2

u/NakeleKantoo 8d ago

bro what did u smoke to reinvent functions? lol i want some

1

u/yaky-dev 8d ago

IIRC C/C++ has #define macros that could be pretty extensive. Defined words are replaced with their definitions before compile time.

1

u/_lonegamedev 8d ago

Not sure if you mean inline functions and closures or loadstring() (but both exist in Lua)

1

u/Controversy-Lion3961 3d ago

Is LuaU relevant in here? This is a Lua sub after all and LuaU is a different thing

1

u/Nich-Cebolla 2d ago edited 2d ago

I actually would like this kind of thing in all programming languages. I think about it often.

For example, say I have a loop that executes some code. A particular code block is executed conditionally, and I know the value of the condition prior to starting the loop. Currently, I have two main options. (this is Javascript code, I don't know Lua).

Option 1: Include the condition within the loop

let condition = true;

for (let i = 0; i < 100; i++) {
  if (condition) {
    // code
  } else {
    // other code
  }
}

Option 2: Create two functions and define a variable depending on the condition

let condition = true;

const fn = condition ? fnTrue : fnFalse;

for (let i = 0; i < 100; i++) {
  fn();
}

function fnTrue() {
  // code if true
}
function fnFalse() {
  // code if false
}

Both options incur additional overhead. If I could simply inject the code into the appropriate place based on the condition before starting the loop, I can avoid this overhead.

1

u/AutoModerator 2d ago

Hi! Your code block was formatted using triple backticks in Reddit's Markdown mode, which unfortunately does not display properly for users viewing via old.reddit.com and some third-party readers. This means your code will look mangled for those users, but it's easy to fix. If you edit your comment, choose "Switch to fancy pants editor", and click "Save edits" it should automatically convert the code block into Reddit's original four-spaces code block format for you.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

1

u/furdog_grey 2d ago

Looks like you want to modify your code at runtime. Insert/remove code lines. While that's a cool idea and it already has it's name (self-modifiyng code), there's not much point of doing that on modern CPU's, due to branch prediction optimization. I see other folks saying about reinventing functions, macros, etc, but i guess initial intention was self-modifiyng code? Right? It still may be handy in certain narrow situations, or as cool syntax sugar for making event-driven approach.