DIP82: static unittest blocks

H. S. Teoh via Digitalmars-d digitalmars-d at puremagic.com
Mon Oct 5 12:28:09 PDT 2015


On Sun, Sep 27, 2015 at 05:01:45AM +0000, Jonathan M Davis via Digitalmars-d wrote:
> This DIP provides a way to handle unittest blocks inside of templates
> which works with ddoc without compiling the unittest blocks into each
> instantiation.
> 
> http://wiki.dlang.org/DIP82

Thanks, Jonathan, for writing this up.  I have felt a need for this for
a long time now, both in Phobos and in my own code.  Overall, I'm very
much in favor of this proposal, particularly as it lets us make use of
ddoc'd unittests within templated types without the associated template
bloat.  This is quite important for Phobos as otherwise, we either risk
code examples in the docs bit-rotting (user annoyance when examples
don't compile, etc.), or user code compiled with -unittest unnecessarily
paying the penalty of multiple identical unittests just because they use
Phobos.

One minor issue with the DIP as currently written, though: it should be
stipulated that inside a static unittest block, it is illegal to
reference template arguments to the enclosing template block.
Otherwise, you may run into pathological cases where it's not clear what
the correct semantics should be:

	template MyTmpl(T) {
		///
		void method() { ... }

		///
		static unittest {
			// Unclear what should be the type of t in the
			// single instance of this unittest:
			T t;
		}
	}

	// (... since MyTmpl likely has multiple different
	// instantiations:)
	alias T1 = MyTmpl!int;
	alias T2 = MyTmpl!string;
	alias T3 = MyTmpl!float;


T

-- 
2+2=4. 2*2=4. 2^2=4. Therefore, +, *, and ^ are the same operation.


More information about the Digitalmars-d mailing list