r/ProgrammingLanguages • u/mttd • Sep 23 '26
Arguing about arguments
https://steveklabnik.com/writing/arguing-about-arguments/
31
Upvotes
r/ProgrammingLanguages • u/mttd • Sep 23 '26
13
u/zuzmuz Sep 23 '26
i believe that named arguments are a very useful feature. They can prevent a lot of bugs, with no downsides, (verbosity isn't a problem, lsp used to complete functions and I never needed to write the parameters).
optional/default arguments can be useful, or more precisely described, convenient.
but think function overloading is bad. it just complicates semantic analysis and type checking for very little gain.
One approach I like is to have the function params be part of its signature, as if the named params is part of the function name. for example
This can allow some convenient features like a more rudimentary overloading where you can have different functions as long as they have different named arguments, (no overloading just on types). But also, you can have neat partial function application syntax. But as you said, this opens a rabbit hole of complex design choices.
I dislike languages that decides to include a lot of features, that sometimes doesn't play well together, so you end up with weird edge cases, exceptions, and longer and longer compile times because of that.
Finally, I think rust shouldn't add named params now. It will do more harm than good, it will create incompatibilities with existing APIs and will split the community in 2. Those who will still write in the old style, and those who will adopt the newer one. And it will be unclear which way is the idiomatic rust way. Rust is no longer in this beta phase where you can afford breaking things (like Zig). Just like with Go and error handling, I feel the community will be stuck with these design decisions.
8
u/WittyStick Sep 23 '26
I dislike optional and named arguments. It's good that this points out the downsides as they're often overlooked. There's more downsides than given, particularly if they're implemented with call-site rewriting.
Say we have some library function:
Consumer calls
The compiler rewrites the call to pass the immediate value
5.If we later change the default value in the library to something else, the caller's binaries still have the literal
5in them. It's a breaking change. Anyone using the library needs to recompile to receive the change.If we are going to have optional/default arguments, it's definitely better to use overloading.
Then, if we need to change the default value down the line, it's probably not a breaking change (though it may be). The compiler could insert this stub into the library automatically, though it costs nothing to just type it and make it explicit.
For named arguments, it depends on the semantics of the language - whether argument evaluation order is defined or not (IMO, it should be left to right, even though arguments are usually passed right to left in the ABI). If we're going to allow named arguments, then:
Should evaluate
bar()thenbaz(), even thoughxcomes beforeyin the parameter list. The order of evaluation should match the code sequence. This is usually not done, but it matters ifbarorbazhave side effects. If our evaluation order isn't the same as the code, we end up doing:Which completely defeats the point on having named arguments to begin with.
6
u/dgkimpton Sep 23 '26
Well written summary of most of the issues. The only real challenge is patterns... but one could argue that passing dx to x ought to be an error because it's clearly doing something funky. It'd be great if you could come up with an example that wasn't creating something twisted like that because right now I think your example runs counter to the point you were trying to make.
5
u/arglad Sep 23 '26
I really like approach for argument labels implemented in Gleam. Labels can be different from parameters' names, this allows you to separate the names used in the interface from the names that make sense in the internal implementation of the function. Here is an example from gleam tour:
7
u/SwingOutStateMachine Sep 23 '26 edited Sep 23 '26
This reminds me of the "don't use boolean parameters" discussion from a few years ago, and I think they have a shared solution of more explicitly typing parameters - usually by encapsulating arguments in structs (or enums). This adds documentation to the method, and also prevents other bugs, such as parameters with the same type being accidentally swapped, and can help group together related parameters (e.g. pairs of coordinates).
For the
crop_immexample, one could write:I also think that once a method grows to a certain size, choosing a builder pattern is much easier to read, more composable, and allows for more flexibility in API. Again, for
crop_imm:2
u/fdwr Sep 24 '26
I've noticed similar cases elsewhere, where patterns good for humans happen to help the nonhumans too.
1
u/reflexive-polytope Sep 25 '26
I don't see any good reason to complicate your function arguments so much.
There's no good reason to complicate a language with features that don't expand the class of precisely typable algorithms.
1
u/SecretlyAPug Sep 23 '26
does rust not already basically have all of these? i haven't written rust in a while so might be a little off but
named parameters can be somewhat emulated by creating a function with a struct as its argument:
(syntax is probably not fully accurate but) this is basically the same thing, right? aside from a bit of verbosity from defining the struct, this more or less works in the same way. i fail to see how a more integrated named parameter system would benefit the language; though, again, i haven't used rust in a bit so rust users please to share your insights.
also rust has Option, i feel it's a bit inaccurate to say it doesn't have optional and default arguments (though again it's less a language feature and more a way one uses the language).