News for hackers. Stuff that matters.
Re:Proposal: expression to create pointer to simple types by liendolucas on Monday April 19, 2021 @10:25AM (#26863063)
>It seems odd that you can't apply & to a function's return value.

Offtopic: Surprisingly I was asking myself this question but if possible in C... Is it?

Re:Proposal: expression to create pointer to simple types by kevincox on Monday April 19, 2021 @10:44AM (#26863271)
No, but in C you can't apply `&` to any stack value and "automagically" pop it onto the heap. Or from another point of view everything in Go is logically on the heap, the compiler just optimizes values that don't have their address taken to live on the stack.

In C:

  int *f() {
    int x = 0;
    return &x;
  }
It works, but it is wrong. The C type system isn't smart enough to realize the lifetime of x in this case. It is not allowed for a function return because C does have the concept of a temporary value so it is disallowed because it is basically always incorrect to do so.

Note that C++ does somewhat allow this with lifetime extension. It is somewhat like what I expected Go to do, except because lifetime extension only extends to the enclosing block it is more of a footgun. With a dynamic tracing garbage collector like Go it not a footgun.

Re:Proposal: expression to create pointer to simple types by gowld on Monday April 19, 2021 @10:42AM (#26863256)
Nope. You can only take a reference to an lvalue, which is (essentially) an expression that is legal to use in the form `my_lvalue = .... Otherwise, there's nothing to take the reference of.

    int* ref1() {   return &1; }
    
    -> error: lvalue required as unary '&' operand
   
    // 

    #include <stdlib.h>
    int alloc()  {
      return *(int*)malloc(sizeof(int));
    }
    int* ref() {  return &alloc();  }

    ->  error: lvalue required as unary '&' operand
You can still be unsafe though, by making a reference to a stack-allocated object and letting it go out of scope :

    int* make_unsafe_ref() {  int a; return &a;  }
    ->  warning: function returns address of local variable
Re:Proposal: expression to create pointer to simple types by masklinn on Monday April 19, 2021 @11:17AM (#26863675)
> You can only take a reference to an lvalue, which is (essentially) an expression that is legal to use in the form `my_lvalue = ….

I mean it could implicitly allocate, that's what Rust does for instance.

Your second and third attempts would not compile though, the first would by returning a `&'static T`.

Re:Proposal: expression to create pointer to simple types by steveklabnik on Monday April 19, 2021 @11:55AM (#26864187)
"implicitly allocate" is slightly misleading, imho. It's promoted to a static. There's no malloc involved.
Re:Proposal: expression to create pointer to simple types by masklinn on Monday April 19, 2021 @12:02PM (#26864275)
> "implicitly allocate" is slightly misleading

Maybe. I just meant that storage is created implicitly (static or stackframe depending on the case), then a reference is created to that.,

Re:Proposal: expression to create pointer to simple types by steveklabnik on Monday April 19, 2021 @12:40PM (#26864775)
Totally, I don't think you're wrong, just like, people read "allocate" in different ways. I wish words were clearer, heh.
Re:Proposal: expression to create pointer to simple types by fanf2 on Monday April 19, 2021 @11:58AM (#26864233)
Yes, if f() returns the type T then you can write

    &(T[]){ f() }
The type in brackets needs to be an array so that if f() returns a struct then the initializer list has the right shape. If T is a simple type then you can drop the [].