Unused function parameters

765 views
Skip to first unread message

Andrew Lytvynov

unread,
Apr 30, 2013, 9:31:19 AM4/30/13
to golan...@googlegroups.com
Hello, gophers.

What is the reason behind allowing unused function parameters (as opposed to disallowing unused variables)?

Currently, this should work:
func dosomething(param string) {
    fmt.Println("param not used")
}

It feels like this should be treated the same way as unused variables are (compilation error). At least I can't imagine any case where it would break valid idiomatic code.

Thoughts?

Andrew Gerrand

unread,
Apr 30, 2013, 9:34:58 AM4/30/13
to Andrew Lytvynov, golang-nuts
Unused variables are always a programming error, whereas it is common to write a function that doesn't use all of its arguments.

One could leave those arguments unnamed (using _), but then that might confuse with functions like

func foo(_ string, _ int) // what's this supposed to do?

The names, even if they're unused, provide important documentation.

Andrew

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Andrew Lytvynov

unread,
Apr 30, 2013, 9:40:28 AM4/30/13
to golan...@googlegroups.com, Andrew Lytvynov
That's pretty much my proposal: only allow them unnamed (_).

I can imagine only 2 cases where they are used:
backward-compatibility for libs
implementing some interfaces

I can't see how any of those cases would be confusing with unnamed parameters.

I'd be glad to check any example of func which would be confusing without parameters' names, though.

Ian Lance Taylor

unread,
Apr 30, 2013, 9:40:09 AM4/30/13
to Andrew Gerrand, Andrew Lytvynov, golang-nuts
On Tue, Apr 30, 2013 at 6:34 AM, Andrew Gerrand <a...@golang.org> wrote:
> Unused variables are always a programming error, whereas it is common to
> write a function that doesn't use all of its arguments.

Think about methods in particular, that must take a certain set of
arguments in order to satisfy an interface.

Ian

Andrew Gerrand

unread,
Apr 30, 2013, 9:42:48 AM4/30/13
to Andrew Lytvynov, golang-nuts
It's a subtle issue. Not one that I feel strongly about, either.

In any case, we can't make the change for Go 1.x:

Andrew

Andrew Lytvynov

unread,
Apr 30, 2013, 9:45:02 AM4/30/13
to golan...@googlegroups.com, Andrew Lytvynov
I agree on it being rather subtle, and i do realize it won't happen any time soon.
Just wanted to hear some opinions.
Thanks Andrew, Ian.

Aaron France

unread,
Apr 30, 2013, 10:10:18 AM4/30/13
to Andrew Lytvynov, golang-nuts
FWIW,

I think it *should* happen if only to be as strict as possibly you can be (since a lot of the other cases of "unused X" are covered) but I'm not sure of the gains.



--

Alexei Sholik

unread,
Apr 30, 2013, 10:14:06 AM4/30/13
to Aaron France, Andrew Lytvynov, golang-nuts
Erlang's approach is to treat "_names" similarly to "_" names. That is, it will argue about unused name, but not about unused _name. This make it possible to preserve the documenting qualities of the parameter names while also documenting the intent of not using the parameter deliberately.
--
Best regards
Alexei Sholik

Andrew Lytvynov

unread,
Apr 30, 2013, 10:16:54 AM4/30/13
to golan...@googlegroups.com, Aaron France, Andrew Lytvynov
I don't think this is the best solution for such small issue. Too much effort for very little gain.
Reply all
Reply to author
Forward
0 new messages