Hacker News new | ask | show | jobs
by maxbond 1368 days ago
Why? The builder pattern is great.
2 comments

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.