__traits(documentation, X)

H. S. Teoh hsteoh at quickfur.ath.cx
Wed Jan 17 22:31:52 UTC 2018


On Wed, Jan 17, 2018 at 02:15:08PM -0800, H. S. Teoh via Digitalmars-d wrote:
[...]
> OTOH, this combination of string import and self-referencing __FILE__
> opens up a host of curious possibilities (all of which already work
> today, btw):
[...]

Another interesting one: breaking through the barriers of
modularization:

	// mod.d
	string getCode(string file = __FILE__)() {
		return import(file);
	}

	// main.d
	import mod;
	void main() {
		pragma(msg, getCode);
	}

Here, module mod gains access to the source code of a file outside of
itself, and it doesn't even need prior knowledge of the filenames.  In
this case nothing interesting happens, of course.  But imagine if
getCode did something else, like extract private symbol definitions, or
ran a syntax checker, or AST transformations, etc.. All sorts of
interesting effects ensue.

Coming back on topic, one can easily imagine a CTFE D parser that
basically implements __traits(documentation) as *library code*. You
won't even need compiler support for that.  Wow.  Skeleton code:

	string traitsDocumentation(alias symbol, string file = __FILE__)()
	{
		enum code = import(file);
		auto s = searchForDocComment(symbol.stringof);
		return s;
	}


T

-- 
Let's call it an accidental feature. -- Larry Wall


More information about the Digitalmars-d mailing list