Interesting Research Paper on Constructors in OO Languages

"Jérôme M. Berger" jeberger at free.fr
Tue Jul 16 10:54:06 PDT 2013


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:jeberger at free.fr
http://jeberger.free.fr
Jabber: jeberger at jabber.fr

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 198 bytes
Desc: OpenPGP digital signature
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20130716/2e5e7842/attachment.pgp>


More information about the Digitalmars-d mailing list