4 ms·
You can actually have type safe buildrs in rust where trying to call the method that consumes the builder fails to compile if not all the arguments are set corr
by swsieber 6y ago
You can actually have type safe buildrs in rust where trying to call the method that consumes the builder fails to compile if not all the arguments are set correctly.
https://github.com/idanarye/rust-typed-builder/blob/master/README.md#rust-typed-builder https://github.com/idanarye/rust-typed-builder/blob/master/R...
- MrBuddyCasino 6y agoAh yes, linear types to the rescue! Nice.
- swsieber 6y agoI think technically it's affine types. In this particular case it comes from really nice generics - you start out with a struct that's generic over all fields - one generic bound per field. And that let's you implement methods depending on what fields have been set. So you could even make a builder with mutually exclusive setters. But you'd probably just want to use an enum in that case for the mutually exclusive options.
- MrBuddyCasino 6y agoI still don't quite understand the generics magic required in order to not generate exponential amounts of code for it to allow random order of arguments, but I won't complain. That capability would have come in handy on Java/Lombok.
- swsieber 6y ago> exponential amounts of code for it to allow random order of arguments The key is that it only generates code for types that actually get used. So if you always call the orders in the same order, it won't generate tons of code. That said, you could actually tweak the type-safe builder to enforce a certain order for calling the methods on the builder. Also, it's not the order of arguments, it's the order of method calls :)