Hacker News
points by troupo 3 days ago | 0 comments
> in any, out any

Completely unrelated: can anyone tell me why language authors introduce in/out/inout function parameters (see also C#).

It feels like this is some rather bad workaround for something that breaks assumptions about parameters being passed into the function.

[–] hxtk 3 days ago
In Go, it’s a question of who owns the allocation. If you return a pointer type allocated within the function, the answer is “no one,” so the pointer escapes and has to be managed by the garbage collector.

If the allocation is owned by the calling function which uses it internally and then doesn’t return it, it can be a stack variable. Or the same allocation can be reused many times in a loop. Or an array of allocations can be made all at once with foreknowledge of the total number.

[–] throwaway894345 3 days ago
If you pass a pointer into a function, the Go compiler can (and sometimes does) allocate it on the heap. Go has no allocation/ownership semantics, tragically, so the best we can do is depend on undefined behavior regarding the escape analyzer and hope the Go team doesn’t break it (which is a relatively safe assumption, considering that it’s unlikely that the Go team would start allocating things on the heap that were previously allocated on the stack—Go rarely regresses performance like this).
[–] ahmedtd 3 days ago
The parameters named "out" here are not "output parameters" in the way you seem to be thinking. They are "input parameters" named "out", because they are the buffer or pipe you want the data written to.
[–] jayd16 3 days ago
`out` params in C# are params that must be set within the function. They are way to implement multiple return values and tell the caller "this is written to, not read from."

But you might be conflating that with in/out for Contra/Co-variance when specifying a generic.

[–] metaltyphoon 3 days ago
> They are way to implement multiple return values

They are one way to implement multiple return values. For multiple returns, tuples can be used. In fact, async methods can’t declare out params

[–] duskwuff 3 days ago
"in" and "out" are the names of the parameters, not qualifiers.
[–] CharlieDigital 3 days ago
C# `out` is kinda nice because it can be `var`d and used directly.
[–] troupo 3 days ago
Erm... what do you mean?
[–] metaltyphoon 3 days ago

  if (int.TryParse(str, out var value) && value == 5)
  {
      // value is available 
  }

   // value also available