In exchange for type safety, it looks as though you have to give up the ability to build the end result in stages. See the comment (@rob) I've just added. Please correct me if I'm wrong.
Rob
On Wed, Jul 9, 2008 at 11:26 PM, Rafael de F. Ferreira <
rafael@...> wrote:
Thanks for the plug. I would really appreciate if anyone has comments
or criticisms to make.
Also, I was wondering if there is any way to remove the boilerplate
from the code. It becomes apparent when looking at the version with
abstract members (http://snippets.dzone.com/posts/show/5741).
Leaving aside the phantom types, it is easy to define a BuilderPart
class to encapsulate each field (a thin wrapper over Option[] that
returns the builder for method chaining); the price is a little
mutable state. But when the types are added to the picture, I can't
think of a way to avoid threading them all in each builder method.
Moreover, when threading the types, I couldn't find a way to avoid
threading the values as well.
PS: Curiously, when looking around for a solution to avoid threading
the values, I implemented a pseudo-dependent Map very similar to the
one Sean proposed earlier today. Just an amusing coincidence :)
PS2: No grains of barley were harmed in the writing of the blog post.
--
Rafael de F. Ferreira.
http://www.rafaelferreira.net/