Hacker News new | ask | show | jobs
by david2ndaccount 1367 days ago
It’s sad that Rust has decided to settle with the builder pattern instead of implementing named arguments.
3 comments

I'm pretty sure that there's no objection to including named arguments in Rust as of yet. It's just that no one has wanted it badly enough to write an RFC for, and implement, named arguments.
Can’t you pass “argument” structs as parameters to kinda emulate the feel of named arguments (along with using the Default trait)? I use this in C++ a lot, and it seems that some Rust devs are doing this too.
You can, but it is a mouthful. Especially if you want optional arguments.

    foo::some_func(foo::SomeFuncArgs{
      a: 8,
      b: Some(false),
      ..Default::default()
    })
Personally I would be really happy if `..` could default to `..Default::default()` which would make this a lot cleaner. But even then needing to name a type for the argument struct is noisy. Language-integrated keyword arguments would make this a lot cleaner:

    foo::some_func(
        a=8,
        b=Some(false))
By the way, C++20 designated initializers works like a charm for this.

  foo::some_func({
    .a = 8, 
    .b = false
  });
(I view this and std::span as the only two usable features in C++20, and pretty much everything else can go to the dustbin or the drawing board.)
I'm waiting to getting some spare time to propose (not quite but very close) that in an RFC. A lot of conversation about it from a while back is available at https://internals.rust-lang.org/t/pre-pre-rfc-syntactic-suga...
Indeed, it's basically the same as in JS/TS where you just pass an object of key/values rather than have named arguments.
Yes, but it is a lot cleaner there because you don't need to specify the name of the type and you don't need to do anything special for optional arguments.

I wouldn't mind if it worked this way "under the hood" but syntax sugar for passing that last argument as keyword arguments would be a fantastic quality of life improvement.

Why? The builder pattern is great.
It gets awkward really fast when you may need to set a value.

    let mut foo = build_foo()
        .with_a(8);

    if need_to_set_b {
        foo = foo.with_b(false);
    }

    let foo = foo.build();
Would look very similar with named arguments, no?
Maybe. It depends a bit on how they are implemented. If there is a good way to pass "default" then you can just have a condition for that one argument. But if there is no way to pass "default" then it can get hairy.
For sure. I would like to see that in Rust as well, that would be nice. I am willing to put up with the lack of variadic & named arguments for the goodness the borrow checker brings me, but I can see how that would improve the ergonomics substantially.
Having both approaches available would be better IMHO. Builder pattern is very nice but I frequently miss having named arguments too. Even if macros can partially fullfill that need, it's not the same and introduces more quirks and complexity...
Problem with named parameters is that they make renaming function params a breaking change.
Unless the IDE doesn’t support this refactoring? It’s ridiculously easy in C# and VS or VSC.
A (public) library doesn't have access to all codebases depending on it.
Even there changing the method param will invalidate semver specification.
Fair enough.