Hacker News new | ask | show | jobs
by cyco130 446 days ago
I've long entertained the idea of a language feature that allows returning unknown sized objects by value. The calling convention would generate two function calls, one to go get the size which would then be passed to alloca to reserve space for the return value and a second call to get the actual result. Not sure of all the implications but I think it might be an idea worth pursuing to make things like variable length arrays and structs with flexible members near-first class constructs in the language. Pseudocode:

   function zero_filled_int_array(length: usize): int[length] {
      ...
   }
The int[length] bit determines the return value of the size call and the main body constitutes the part that actually fills in the value.
1 comments

In my understanding, Ada allows you to return unsized values, but it's generally implemented via a secondary stack.
Gold mine! Didn't know about to concept, thanks.
You're welcome!

You also might be interested in this old RFC for Rust, which described something similar https://github.com/PoignardAzur/placement-by-return/blob/pla...

And now I’m proud because that’s pretty much the exact idea I tried to pitch at the great grandparent.