Hacker News new | ask | show | jobs
by killercup 2643 days ago
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`)