Skip to content

Optional (or default) function arguments #1675

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
gopherbot opened this issue Apr 8, 2011 · 8 comments
Closed

Optional (or default) function arguments #1675

gopherbot opened this issue Apr 8, 2011 · 8 comments
Labels
FrozenDueToAge LanguageChange Suggested changes to the Go language

Comments

@gopherbot
Copy link
Contributor

by pierre.gotab:

Summary of mailing list related subjects:
There is no optional arguments in Go. (And no function overloading.)
- So this is difficult to port some APIs in Go
- And difficult to write some code (as strings.Split 3rd arg)

Proposed existent workarounds:
Assume we want a function foo taking two integers with the second optional defaulting to
0.

- function wrapper
func foo2(bar int, baz int) {
    // sthg
}
func foo1(bar int) {
    return foo2(bar, 0)
}

Need to write the same thing 3 times or more... Somewhat against the "everything is
declared exactly once" Go principle.

- varargs
func foo(bar int, optional ...int) {
    var baz int = 0
    if len(optional) > 1 {
        panic("Too much args")
    } else if len(optional) == 1 {
        baz = optional[0]
    }
    // sthg
}

Heavy code, catch "Too much args" at execution time.

- struct
type fooArgs struct { baz int }
func foo(bar int, optional fooArgs) {
    baz := fooArgs.baz
    // sthg
}
// call:
foo(3, fooArgs{42})
foo(3, fooArgs{})

Make the call heavy to write and create a struct each call.

Proposed optional arguments specification:
- With default value
func foo(bar int, baz int = 0) {}
func foo(bar int, baz := 0) {}

- Using the zero value
func foo(bar int, baz? int) {} // baz will default to 0 if not set

- With named parameters
func foo(bar int, baz int = 0, qux int = 0) {}
foo(3, qux:5)

The named parameter with default value approach is close to the struct workaround, and
is really flexible.
@robpike
Copy link
Contributor

robpike commented Apr 8, 2011

Comment 1:

Labels changed: added priority-low, languagechange, removed priority-medium.

Status changed to Thinking.

@rsc
Copy link
Contributor

rsc commented Dec 9, 2011

Comment 2:

Labels changed: added priority-someday.

@robpike
Copy link
Contributor

robpike commented Dec 12, 2011

Comment 3:

Status changed to WorkingAsIntended.

@gopherbot
Copy link
Contributor Author

Comment 4 by crifan2003:

prefer:
With named parameters
or:
With default value
if go will not tend to support this common and useful feature, I would be very
disappointed ..

@gopherbot
Copy link
Contributor Author

Comment 5 by pierre.gotab:

Unfortunately, the status WorkingAsIntended seems to say "we do not want this kind of
feature", and one of the reasons why I went away from Go.

@adg
Copy link
Contributor

adg commented Sep 23, 2013

Comment 6:

Optional arguments are often a crutch for poor API design. You tend to think "Oh well,
we can always add it later as an optional argument." Or (worse) "Just add it as an
optional argument." The result (as demonstrated by many Python programs) is functions
that take many optional arguments whose semantics are hard to discern.
Go has design goals, which are discussed at length in this article:
http://talks.golang.org/2012/splash.article
In reading that article, it should become clear why optional arguments are a no-go for
Go.
If you do not share those goals, then you are probably better off using another
language. Sorry.

@gopherbot
Copy link
Contributor Author

Comment 7 by pierre.gotab:

I understand the pitfalls of optional arguments. But forbidding them because they allow
poor design is not a solution to me. Because when you really need them (and it happens
quite often) you must use one of the "tricks" I mentioned in my first post.
When you write two or three or more functions that call each other to support every
usecase, you write a lot of stupid code. My proposition is just to let the compiler
write this code for you. This is not bad design, it's just syntactic sugar.
But maybe you are considering it's up to a project like Groovy or CoffeeScript to do
this job for us?
And finally, I found no argument about this in the link you gave to me :(

@rsc
Copy link
Contributor

rsc commented Sep 23, 2013

Comment 8:

The [email protected] mailing list is a better place for discussions.

Labels changed: added restrict-addissuecomment-commit.

@gopherbot gopherbot added workingasintended LanguageChange Suggested changes to the Go language labels Sep 23, 2013
@golang golang locked and limited conversation to collaborators Dec 8, 2014
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge LanguageChange Suggested changes to the Go language
Projects
None yet
Development

No branches or pull requests

4 participants