Now, if the order of parameters of a function changes, or if it
gains a parameter, I have to change all function calls. Or I can
call a function with parameters in the wrong order, if they have
the same type, and get untraceable errors.
package main
import "fmt"
type t struct { a, b, c int }
func f(a, b, c int) int {
return a + 2 * b + 3 * c
}
func main() {
fmt.Printf("%+v\n", t{b:5}) // prints: {a:0 b:3 c:0}
// fmt.Printnl(f(b:10)) // won't compile
}
--
Peter Kleiweg
http://pkleiweg.home.xs4all.nl/
Use a struct literal:
Connect( "localhost", Options{usr: "ddd", pass: "", port: 3306})
with a suitable declaration for Options. Now the "optional parameters"
are values and can be passed around, copied,
etc and co.
(I'm not convinced that named-argument and default-argument
machinery is worth the investment. Amonst other things, it makes
it easier to end up with ridiculously long parameter lists because
"you only write what you need". The function still needs to /make
sense/ with all those arguments. You also need to make sure that
you can write types for these kinds of functions.)
Chris
--
Chris "allusive" Dollin
...
> with a suitable declaration for Options. Now the "optional parameters"
> are values and can be passed around, copied,
> etc and co.
>
> (I'm not convinced that named-argument and default-argument
> machinery is worth the investment. Amonst other things, it makes
> it easier to end up with ridiculously long parameter lists because
> "you only write what you need". The function still needs to /make
> sense/ with all those arguments. You also need to make sure that
> you can write types for these kinds of functions.)
>
> Chris
>
I'll note that Javascript doesn't have named arguments, so it is a very
common idiom to make the last parameter a map/dict/... so that you can
supply named parameters. So you get
CallFoo(12, 13, {"a": b, "c": d})
While that works, it has the feeling of not being really supported by
the language. And it is a bit more verbose in Go:
map[string][string]{"a": b, "c": d}
Or you define an Options for each function that wants them, and somehow
avoid name collisions, etc. But then you get:
CallFoo(12, 13, CallFooOptions{a: b, c: d})
I suppose you get to avoid the quotes this way.
It still feels like the long-way-around for a language that already has
named parameters (just only at certain times).
John
=:->
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (Cygwin)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
iEYEARECAAYFAk4VlBMACgkQJdeBCYSNAAMHmQCfbpGru1MrJQ8zlWepUspX+kyx
vRkAoJPcTgz4YeeAZ1+gOAvbsB7hg+gH
=UDfs
-----END PGP SIGNATURE-----
> I'll note that Javascript doesn't have named arguments, so it is a very
> common idiom to make the last parameter a map/dict/... so that you can
> supply named parameters.
And Ruby has special syntactic sugar for this (maps as last arguments
to functions).
> And it is a bit more verbose in Go:
> map[string][string]{"a": b, "c": d}
> Or you define an Options for each function that wants them,
Yes; then you get type-checking and the ability to treat
those Options as values in their own right.
> and somehow avoid name collisions, etc.
In the usual way, I imagine.
> But then you get:
> CallFoo(12, 13, CallFooOptions{a: b, c: d})
> I suppose you get to avoid the quotes this way.
One would not call it CallFooOptions, and probably not FooOptions
either.
...
>> But then you get:
>> CallFoo(12, 13, CallFooOptions{a: b, c: d})
>> I suppose you get to avoid the quotes this way.
>
> One would not call it CallFooOptions, and probably not FooOptions
> either.
>
> Chris
>
Perhaps. But doesn't that restrict you to having only 1
"named-parameter" struct per package, rather than 1 per
function-that-wants-named-parameters?
John
=:->
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (Cygwin)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
iEYEARECAAYFAk4VmQoACgkQJdeBCYSNAAN0jgCfTw0yh+8rvl+sE068Rxt1lOBs
LEcAoJDhaeiM2ZHNgk5YIsGCVb3SJCwU
=AUyz
-----END PGP SIGNATURE-----
When you've got so many function parameters that you feel a need to
give them names and default values, start thinking about whether they
might be reused in some ways, and how you'd group them such that they
*can* be reused.
The recent rewrite of the exec package is a nice example. There was a
function with an ever-increasing number of inputs and outputs, most of
which were usually unused. Now it's a struct, with a single helper
function to generate it in the most common case. But it's also no
longer an argument to a function, since you can now treat it as a
first-class data object, and can make copies of it and modify it to
your heart's content. A classic case where named arguments might have
been desired, where we got something better and more powerful instead.
--
David Roundy
When you've got so many function parameters that you feel a need to give them names and default values, start thinking about whether they might be reused in some ways, and how you'd group them such that they *can* be reused.
There are cases where X is legitimate for just about every
feature anyone has ever proposed. That doesn't mean we
have to put every feature anyone has ever proposed into Go.
Russ
If you are wrapping a library and not changing its interface, you cannot expect the code to be clean because it was designed for a language very different from Go. If you redesign the library to have a few basic types that have the methods on them, then you can have some very simple, concise things like:&Dialog{Closeable: true,Title: "Your Go installation is awesome",Buttons: []string{"ok", "thanks"},}.Show()
Looks like this is a syntactic infelicity; it seems that
it is parsing as &(Dialog{...}).Show() rather than
(&Dialog{...}).Show(). I can see why, but it's a shame.
Looks like this is a syntactic infelicity; it seems that
it is parsing as &(Dialog{...}).Show() rather than
(&Dialog{...}).Show(). I can see why, but it's a shame.
>
>
> > There are cases where X is legitimate for just about every
> > feature anyone has ever proposed. That doesn't mean we
> > have to put every feature anyone has ever proposed into Go.
>
> No need for that, I remember the famously unfriendly FAQ #14 :-)
> Nobody suggests that you should do that.
>
> I bet that you get the same questions over and over: Generics,
> overloading, tail calls, anonymous function recursions, boolean
> evaluation of all terms, named function arguments. But people do not
> bug you because they do not like the language, or because they do not
> read what you wrote earlier. For some of these decisions, they do not
> see a convincing argument (i.e., not something like "overloading could
> slow down the compiler", or "generics are only a convenience to the
> programmer").
>
> It would be great if you could set aside a corner where people can
> discuss features and possible extensions, and actual arguments are
> exchanged. Ideally, there might even be a defined process for that, so
> people like me can look up the ticket and see the current state of
> your decisions and reasoning.
My 0.02, IMHO and whatnot:
This mailing list seems to serve for discusssing "features and possible
extensions". Actual arguments are awesome. They're rarely accepted,
though, and in a language which is designed to be simple that's
probably a good thing. I doubt changing the venue would alter that.
I don't think I remember the position explained by the language devs
on this mailing list for any of those feature requests varying since
the language was released. Some kind of FAQ for common feature
requests might be useful just to discourage reviving discussions
without having new ideas regularly. Google Groups archives, so it could
readily link to previous discussions if they were felt to contribute.
I don't think conflating public discussion with dev reasoning is
accurate or wise, though.
> It would be great if you could set aside a corner where people can
> discuss features and possible extensions, and actual arguments are
> exchanged. Ideally, there might even be a defined process for that, so
> people like me can look up the ticket and see the current state of
> your decisions and reasoning.
I think we can use the wiki for that.
http://code.google.com/p/go-wiki/
Ian
I assume you are referring to
http://golang.org/doc/go_faq.html#Why_doesnt_Go_have_feature_X
I can't see what is unfriendly about that.
Russ
#14 simply dismisses all these ideas out of hand, treating them as not
warranting any further discussion. It is always unfriendly to crush
peoples hopes and dreams without giving the appearance of even
listening.
It might very well be that all of your design decisions are indeed for
the best.
You have failed to understand the essence of time management.
> You have failed to understand the essence of time management.
Russ, thank you for the kind advice. I will check back into this
discussion next time I need feedback on how I fail or what I should
know about time management.