by codedokode · 4y ago · 0 comments
1) named arguments:
createFoo(barness: "bar", bazness: True)
2) struct with default values:
createFoo(FooConfig{barness = "bar"})
Go might not have these features, but I guess it is easier to add them than to invent weird "function returns function" tricks.
With functional options the code needs to be duplicated: first, you need to define a field in a struct and then a functional option that sets that field to a given value. With ideas above no duplication is necessary.
Although I hate functional options for their lack of discoverability, the idea that currying is "awful" is pretty far fetched.
Currying in languages that use different syntax is orthogonal to the point being made here.
For example, compare these two functions:
function (a, b) { return a + b; }
function create_adder(a) { return function(b) { return a + b;}}
The purpose of first function is easier to understand. And if you rewrite last function according to modern trends, it can become even less readable:
const create_adder = (a) => (b) => a + b;
Also, if you start a function with a word 'const' then it is less obvious that this is a function and not just a constant. In contrast, if you use a word 'function', then you understand what it is from the first word.
If we had non-zero default values or named arguments, this pattern wouldn't exist.
Need to add functionality to something? Don't think! Just add an argument with a default to the current behavior, all the way up and down the stack.
Now you have just one API that does everything! Just set 20-40 parameters to decide the behavior.
What I don't like though is when all those options are hidden behind two-stars keyword argument and it is difficult to understand what options are allowed, what they mean, what type they have and so on:
def example(**options): ...
Hey, that's basically how any function with more than one argument is implemented in Haskell. (Look up currying.)
It's not so much that this style is 'awful' in any universal sense; it's more that Go is terrible, terrible host language for anything in this style.
add x y = x + y
There is no functions returning functions here, just a normal addition. It is perfectly readable.
Haskell just decided to have a syntax (and type system) that can handle this without inducing nausea in the programmer.
As with everything, this comes with trade-offs. For example, Haskell's syntax (and type system) have a much harder time dealing with optional arguments. Something that eg Python does just fine, and that even C handles OK in the form of varargs.
prevVerbosity := foo.Option(pkg.Verbosity(3))
foo.DoSomeDebugging()
foo.Option(prevVerbosity)
(better yet, use defer to restore)Your examples seem to be reducing the problem space to exclusively object creation time. And in that case, yes named params or a struct with default values work great. But they work a lot less great when you're talking about changing an existing object, as now your default values can't just be the actual default values, but rather optional values since you need to distinguish between "set to a value that happened to be the same as the default" and "didn't set a value at all, so don't change it".
If you really need a general scoped override, it could be done in the config struct approach just by copying and restoring the entire config. This might be expensive if the struct is big, but on the other hand, you could change multiple properties at once which doesn’t look possible in the function-based approach.
The source of this pattern per the article https://commandcenter.blogspot.com/2014/01/self-referential-...
The example used is trace verbosity, which is both useful to do a scoped override and cheap to do.
For scoped logging, the obvious approach would just be to add a "setVerbosity" method to your object. You can try to do it in the most general way possible, sure; but that feels similar to building lots of generic data structures and other abstractions, which is something that Go intentionally resisted for a long time (on the basis of keeping things simple and concrete, rather than building ivory towers of abstractions).
Anyway, the "Functional Options Are Slow" article reads like a post-hoc justification written by somebody who never liked functional options in the first place anyway -- i.e., the stuff in the final section. The real argument is "I think functional options are a bad idea for style and usability, but if that doesn't convince you, you can also look at the raw performance numbers, and it turns out it's really expensive too." Which I think is a good argument!
CreateFoo(FooConfig{bar: “baz”})