you are viewing a single comment's thread
view the rest of the comments
[–] 10 points 6 months ago (1 child)

Not saying that named parameters are bad, but the builder pattern serves the same purpose and imo it's more ergonomic.

TrainBuilder::new()
  .with_electric_motor(true)
  .with_width(1.0)
  .build()

I don't care about the color of the train or the amount of wheels, I just want the default train with a few changed parameters.

If you do named parameters in rust, you would need to set every parameter. The only way to not set every parameter is to give a special meaning to the Default trait. But that is uncommon to happen in rust. And many structs that could easily derive Default don't.

  • source
  • parent
  • hideshow 1 child comment
  • [–] 1 point 5 months ago

    If you do named parameters in rust, you would need to set every parameter.

    If that's the condition, yeah, it's bad. The good thing about how Python handles it is that they can still be mandatory or optional. And that makes it a more flexible option than the builder pattern because it applies to any function or method.

  • source
  • parent