Intro
Rust has a very rich type system. It also has move semantics. Using these two features together you can build your APIs using the ...
For further actions, you may consider blocking this person and/or reporting abuse
I enjoyed this a lot, here's a java adaptation relying on downcasts and type erasure to accomplish mostly (slightly less ergo) the same effect.
Really nice! Thank you! The
static ReadyToLaunchRocket build
trick is really clever!I'm not convinced this is a better way. Like you said, the compilation error is an issue. Also, while the call itself might be more readable, if you turn to read the trait to understand what it does, you're in for a world of pain- so much noise. And third, it won't scale: if I want to cook pasta a million times, there's a lot of unneeded copies and allocations. I think the real solution should be in the language itself, with named and default arguments: internals.rust-lang.org/t/pre-rfc-...
I agree, the copies are ugly. I expect them to be optimized away by the compiler tough (I haven't checked, it should be an interesting thing to do).
Named and default arguments just postpone the problem. If you have many optional fields your code will likely include a lot of
if Some(val)...
to handle them.These
ifs
will be a runtime penalty - unless the compiler can again optimize away. Using types you can achieve static dispatch and incur in no runtime penalty (at expense at slower compilation and bigger code size).Did you consider
to reduce the lines of code needed for
CookPastaBuilder
methods?