2 ms·
I mean that this: func MyOption(arg int) Option { return myoption(func(opts type) { opts.thing = arg }) } will result in this: MyOption(5)
by Groxx 7y ago
I mean that this:
func MyOption(arg int) Option {
return myoption(func(opts type) { opts.thing = arg })
}
will result in this:
MyOption(5) == MyOption(5) // false
Which makes it a pain in 1) testing, since you can't see the options that were passed, and 2) middleware, since you can't see the options that were passed, to validate or extend them safely. And 3) debuggers or Printf since you can't see the value in the closure until it's executed.
Instead do:
type myOption int
func MyOption(arg int) Option {
return myOption(arg)
}
since it has none of those problems.
Whether you use the "loop and opt.apply(arg)" pattern or not doesn't really matter - that's a purely internal detail (personally I find it over-simplifies things and causes problems). Just "avoid passing values through func closures".