27
Arguing about arguments
(steveklabnik.com)
Welcome to the Rust community! This is a place to discuss about the Rust programming language.
Credits
Steve doesn't really elaborate on the drawbacks to the features he listed, just kinda says he doesn't like them. I'll do some of that here:
def foo(a=[]), append toaon each call, and use that data on future calls tofoosince the same list is provided as a default each time. In C#, default values are compiled into the caller's code, meaning if the caller's code was developed for an older version of a library and you use it with an updated version that has a new default, the caller will still use the old default. And so on, languages all handle defaults differently.params Foo[] argsequivalent wouldn't be very useful, but varargs with variadic types would be powerful, just I assume very difficult to implement (for examplefn foo<...Ts>(blahs: Ts...)or something, whereTsis a variadic tuple). In any case, that's a feature many libraries would take advantage of (axum, bevy, etc).Yeah, which especially becomes an issue in languages with poor mutability controls. Probably the most benign variant of argument defaults would be restricting them to being
const, or at the very least immutable including internal mutability. Instead we have situations like in Python where it's necessary for linters to warn people that setting[]or some other mutable data structure as a default is a bad idea (e.g. B006).Yeah, this is also something of a mixed bag, where
T, but it's used for different purposes,s/blacklist/blocklist/) because it would break clients is just a PITA.I wrote in another comment that I was partial to named arguments out of habit, but the more I think on it, I wonder if it's not mostly a tool to work around missing type information and bad APIs, which, uh, may not be the most desirable thing to add to Rust.