3 ms·
Keyword arguments with default values is the one Python feature that makes my Go code more verbose than my Python code. def f(a=11, b="some default", c=55.5,
by fauigerzigerk 13y ago
Keyword arguments with default values is the one Python feature that makes my Go code more verbose than my Python code.
def f(a=11, b="some default", c=55.5, d=D()):
#...
f(c=44.4)
becomes
type Config struct {
a int
b string
c float
d interface{}
}
func f(c *Config) {
a := 11
if c.a != 0 {
a = c.a
}
b := "some default"
if c.b != "" {
b := c.b
}
c : = 55.5
if c.c != 0.0 {
c := c.c
}
d := new(D)
if c.d != nil {
d := c.d
}
//...
}
func main() {
f(&Config{c: 44.4})
}
- nknighthb 13y agoI just solved a similar problem in a slower but fairly general way. It's not appropriate everywhere, but it works well for the use-case I built it for. I can't share the actual code, unfortunately, but by using struct field tags, reflect, and json, you can end up with code that looks something like this: type FArgs struct { A int `def:"11"` B string `def:"\"some default\""` } func f(margs map[string]interface{}) { var args FArgs SetupArgs(margs, &args) // ... } If f() is being called in a tight loop, your overhead can be enormous, but outside performance-critical code, it's probably good enough.
- fauigerzigerk 13y agoYou're not just losing performance but also static type checking.
- nknighthb 13y agoNot really. If the passed-in or default value doesn't convert to the field's type, SetupArgs panics. Edit: Variations on this that offer compile-time type checking of passed-in values are possible, too. My use case wouldn't work well with that, though, because the function needs to be called by other functions that would have no clue about the FArgs struct. You could instead do a version that just swaps in a default for any nil fields. And in any case, you've got a lot more safety than the original Python code, no?
- fauigerzigerk 13y ago"And in any case, you've got a lot more safety than the original Python code, no?" Only insofar as your approach puts the values back into a struct that can subsequently be used in a type safe manner. Arguably that's better than having no type safety at all. But I think my main gripe with your design is that it is not just less type safe and slower, it also comes with more mental friction and verbosity than what Python does. Granted, it's a lot less verbose than my Go code, which makes it a good solution in some scenarios. But in my view it's not a good general replacement for default keyword args and you didn't claim it was.
- nknighthb 13y agoI certainly wasn't arguing it was a perfect substitute for default args, just that it was easier to manage than the example you gave. The application I'm currently working on is mostly a direct port of Python code to Go. Python code I originally wrote. You needn't tell me it fails to accomplish all the things Python's default args do.
- Someone 13y agoThat code is broken; the observation that a field has its default value does not imply that no value was set. Test case: func main() { f(&Config{a: 0, b:"", c:0.0, d:nil}) } That calls f(11,"some default",55.5,D()) => It is even harder to replicate that Python idiom (I wouldn't know how, but I have only glanced at the go language spec. It would help if you could auto-initialize structure members with values only known the the structure itself, or if one could replace that &Config above by a function call, like this: makeConfig(){c: 44.4})
- fauigerzigerk 13y ago"That code is broken; the observation that a field has its default value does not imply that no value was set" That doesn't necessarily mean the code is broken. It may or may not be possible in a particular situation to treat the zero value as semantically special as I have done. But if it's not, then you are of course right that this creates an additional problem. Using a seperate function to initialize a Config is fine, but you would actually have to create a makeConfigForF function because the defaults are specific to f and not to Config. It's all messy. That's why I like keyword default arguments.
- rogpeppe1 13y agoi usually do something like this instead: type Config struct { a int b string c float d interface{} } var defaultConfig = Config{ a: 11, b: "some default", c: 55.5, d: new(D), } func f(cfg Config) { // ... } func main() { cfg := defaultConfig cfg.c = 44.4 f(cfg) } having the "keyword" arguments as a separate type makes them potentially useful as a currency to pass to other functions too, rather than as a set of attributes and values defined for one function only.