Unnamed Parameters in Function Declarations

2,504 views
Skip to first unread message

danr...@gmail.com

unread,
Jul 14, 2011, 11:13:38 PM7/14/11
to golan...@googlegroups.com
According the specification, declaring a function with unnamed parameters is valid, like:

func test(int, int) {
}

This compiles, but how are these parameters referenced within the function?

jimmy frasche

unread,
Jul 14, 2011, 11:15:42 PM7/14/11
to golan...@googlegroups.com
They aren't. They're for when you need a function of a certain
signature but do not need to use one or more of the required
parameters.

bflm

unread,
Jul 15, 2011, 2:30:02 AM7/15/11
to golang-nuts
On Jul 15, 5:15 am, jimmy frasche <soapboxcic...@gmail.com> wrote:
> They aren't. They're for when you need a function of a certain
> signature but do not need to use one or more of the required
> parameters.

In the case of unnamed parameters it would be not "one or more" but
"none of the" parameters. Such function thus has no need to have
params at all.

The form is used instead e.g. for situations like this:

var f func(int, int) int
f = func(x, y int) int {
// can call 'f' here
}

Steven Blenkinsop

unread,
Jul 15, 2011, 11:26:01 AM7/15/11
to bflm, golang-nuts
On Friday, July 15, 2011, bflm <befeleme...@gmail.com> wrote:
> On Jul 15, 5:15 am, jimmy frasche <soapboxcic...@gmail.com> wrote:
>> They aren't. They're for when you need a function of a certain
>> signature but do not need to use one or more of the required
>> parameters.
>
> In the case of unnamed parameters it would be not "one or more" but
> "none of the" parameters. Such function thus has no need to have
> params at all.

Yes, the form for eliding individual names requires and explicit _

func (_ int, y int) int

However, normally the names are left in the signature, even if unused,
for the purposes of documentation.

jimmy frasche

unread,
Jul 15, 2011, 1:47:10 PM7/15/11
to Steven Blenkinsop, bflm, golang-nuts
> Yes, the form for eliding individual names requires and explicit _
>
>  func (_ int, y int) int

negative: this compiles perfectly well:

func f(int, _ int, i int) {}

Frits van Bommel

unread,
Jul 15, 2011, 2:13:13 PM7/15/11
to jimmy frasche, Steven Blenkinsop, bflm, golang-nuts

Sure, and so does this:
func f(int, _ int, i int) int {
return int
}

While this does not:
func f(int, _ int, i int) int {
var a int
return a
}

You didn't elide the first parameter name, you just named your first
parameter 'int' :).
(Which makes it unavailable as a type name inside the body of that function)

Note that the spec says you either name *all* of your parameters or
*none* of them.

Ian Lance Taylor

unread,
Jul 15, 2011, 2:19:24 PM7/15/11
to jimmy frasche, Steven Blenkinsop, bflm, golang-nuts
jimmy frasche <soapbo...@gmail.com> writes:

I believe that will give you a parameter named "int", which is probably
not what you want.

Ian

jimmy frasche

unread,
Jul 15, 2011, 2:32:06 PM7/15/11
to Ian Lance Taylor, Steven Blenkinsop, bflm, golang-nuts
Huh, I must be misremembering something, or did it work like I said at
one point and I missed the change?

Russ Cox

unread,
Jul 18, 2011, 10:34:59 AM7/18/11
to jimmy frasche, Ian Lance Taylor, Steven Blenkinsop, bflm, golang-nuts
On Fri, Jul 15, 2011 at 14:32, jimmy frasche <soapbo...@gmail.com> wrote:
> Huh, I must be misremembering something, or did it work like I said at
> one point and I missed the change?

I think you just misunderstood.

> func f(int, _ int, i int) {}

This is no different than

func(x, y int, z int) {}

except for the names of the parameters.

It has been that way since we launched Go.

Russ

Reply all
Reply to author
Forward
0 new messages