Regan Heath wrote: > Or, perhaps another way to ask a similar W is.. can the compiler > statically verify that a create-set-call style object has been > initialised, or rather that an attempt has at least been made to > initialise all the required parts. > Here's a way to do it in Scala: http://blog.rafaelferreira.net/2008/07/type-safe-builder-pattern-in-scala.html
Basically, the builder object is a generic that has a boolean parameter for each mandatory parameter. Setting a parameter casts the builder object to the same generic with the corresponding boolean set to true. And the "build" method is only available when the type system recognizes that all the booleans are true. Note however that this will not work if you try to mutate the builder instance. IOW, this will work (assuming you only need to specify foo and bar): > auto instance = builder().withFoo (1).withBar ("abc").build(); but this won't work: > auto b = builder(); > b.withFoo (1); > b.withBar ("abc"); > auto instance = b.build(); Something similar should be doable in D (although I'm a bit afraid of the template bloat it might create…) Jerome -- mailto:jeber...@free.fr http://jeberger.free.fr Jabber: jeber...@jabber.fr
signature.asc
Description: OpenPGP digital signature