Re:The Phantom Builder in Rust
by saghm on Thursday March 28, 2019 @05:13PM
(#19516684)
For those who want to use the builder pattern in Rust but don't want to write the boilerplate, I've found the typed-builder crate (https://github.com/idanarye/rust-typed-builder) to be an excellent solution. It ensures (at compile time!) that you have initialized all the fields of your struct that are not explicitly marked as having default values.
Disclaimer: I don't have any affiliation with this crate; I'm just a big fan
Re:The Phantom Builder in Rust
by tomsmeding on Thursday March 28, 2019 @03:28PM
(#19515681)
For anyone else who's confused how this presents an alternative to the builder pattern in the post: Rust's std::default::Default trait can be implemented for any struct (also automatically derived if you just want the members' standard default values), and then an instance of the struct can be created with the following syntax: [1]
let instance = YourStruct { foo: 42, ..Default::default() };
where 'foo', and possibly more fields, are the ones you want to override from the default. The rest gets taken from the Default trait specialisation.
Tangential: in Haskell, record types allow a similar sort of trick. If you have a data type like the following:
data D = MakeD { a :: Int, b :: Float }
and have a instance of that data type with your default values:
defaultD = MakeD { a = 42, b = 3.14 }
then you can create another instance of D by selectively overriding stuff from the already existing instance using similar syntax:
Re:The Phantom Builder in Rust
by killercup on Thursday March 28, 2019 @04:09PM
(#19516085)
The simple builder pattern often looks like a lot of boilerplate, I agree. But you can easily crank the coolness of it up a notch by using one of these properties:
- Hide the fields of your struct to not break your users when adding/changing fields
- automatically convert data in your setters (accept a reference to a string even though you want to store a file system path)
- use type parameters to conditionally set and validate values at compile time (by making your data type generic and implementing some setters so that they return a concrete value for that allowing you to write _different_ setters based on that type)
- by using the session type pattern to disallow incorrect method calls at compile times (e.g. calling `set_content` after calling `submit`)
Re:The Phantom Builder in Rust
by ungzd on Thursday March 28, 2019 @03:47PM
(#19515858)
For this example, builder pattern is definitely overkill, but builders can be still useful for more complex cases. Even for simple structs, sometimes you don't want to expose constructor, for example to add additional checks ("smart constructors" in Haskell jargon).
Re:The Phantom Builder in Rust
by spectramax on Thursday March 28, 2019 @05:03PM
(#19516569)
There comes a point when one recognizes that colors and fancy fonts impede legibility and readability of the code. Skyblue/cyan colors for code against white background is frankly annoying to read to the point where I closed the window right away.
Disclaimer: I don't have any affiliation with this crate; I'm just a big fan
Tangential: in Haskell, record types allow a similar sort of trick. If you have a data type like the following:
and have a instance of that data type with your default values: then you can create another instance of D by selectively overriding stuff from the already existing instance using similar syntax: which is equal to MakeD { a = 100, b = 3.14 }.[1]: adapted from https://doc.rust-lang.org/std/default/trait.Default.html
- Hide the fields of your struct to not break your users when adding/changing fields
- automatically convert data in your setters (accept a reference to a string even though you want to store a file system path)
- use type parameters to conditionally set and validate values at compile time (by making your data type generic and implementing some setters so that they return a concrete value for that allowing you to write _different_ setters based on that type)
- by using the session type pattern to disallow incorrect method calls at compile times (e.g. calling `set_content` after calling `submit`)