Three Unlikely Successful Features of D

Adam D. Ruppe destructionator at gmail.com
Thu Mar 22 07:42:22 PDT 2012


On Thursday, 22 March 2012 at 04:49:24 UTC, Nick Sabalausky wrote:
> And then later on someone made sort of a mini file-system where 
> you could add data to a ROM
> image and then query/access it from your code

Now, this reminds me, what if you want to access the
compile time files from runtime?

For instance, one of my projects lets you include other
files in the html. (Something I actually don't like, but
it is an easy crutch.)

You can also replace these at runtime, so the other team
members can change stuff without access to the compiler.

Here's what I ended up writing:


// filesystem alternatives...
string getFile(string name)() {
     if(std.file.exists(dir ~ name))
         return std.file.readText(dir ~ name);
     else
         return import(name);
}

// runtime fetching
string getIncludeFile(string name) {
	enum manifest = ["file1.html", "file2.html", ......];
	string manifests() {
		string cases;
		foreach(f; manifest)
			cases ~= "case \""~f~"\": return getFile!(\""~f~"\");";
		return cases;
	}
	switch(name) {
		default: assert(0, name);
		mixin(manifests());
	}
}



I'm a bit disappointed that I needed a list of files duplicated
there. With import(), if it is in the import directory, it just
works, but then the name must be known at compile time.


This is the best I have so far... and i like it,
but it isn't quite ideal yet.


More information about the Digitalmars-d mailing list