Skip to content

proposal: Go2: automatically take address of pointer parameters (reference arg behavior) #33088

Closed
@rcoreilly

Description

@rcoreilly

[Apologies if this is a dupe -- couldn't find it.]

This proposal is to automatically take the pointer address (i.e., apply the & operator) of any non-pointer parameters that are passed to function arguments declared as pointers. This would make the behavior for the rest of the arguments consistent with the behavior for the receiver arg in a method.

When calling a method that is declared with a pointer receiver, the compiler automatically takes the address of a non-pointer variable.

Likewise, it would be more consistent, and convenient, if the compiler also automatically took the address of non-pointer parameters passed to pointer args. This would effectively mimic the behavior of the reference type from C++, without introducing any new syntax, and would not affect the ability to compile existing Go1 code as it is just turning what is currently an error into a valid function call.

Example:

func (r *MyType) MyFunc(oth *OtherType) {
...
}
...
    a := MyType{}
    b := OtherType{}
    a.MyFunc(b)  // compiled as: a.MyFunc(&b)
...

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions