On Wed, Sep 15, 2010 at 22:19, Nick Sabalausky <a...@a.a> wrote: > One thing that could be used for that is the eval() function I've recently > added to my SemiTwist D Tools library: > > > http://www.dsource.org/projects/semitwist/browser/trunk/src/semitwist/util/process.d > > Example: > ----------------------------- > import semitwist.util.all; > void main() > { > auto x = eval!int(q{ return 21 * 2; }); // String can be > runtime-generated > assert(x == 42); > > eval!void( q{ writeln("Hello World"); }, q{ import std.stdio; } ); > } >
Oh, interesting! I had a look at your code. I like this part: code = boilerplate.format(imports, TRet.stringof, code); Inside boilerplate, I wonder if it's possible to test for auto ret = _main() inside a static if(is(typeof( )))? That way, if _main() 'returns' a void, the static if won't check and you know you have a void return value. > ----------------------------- > > It does require dmd and rdmd to be on the path. > > I hadn't posted anything about it before because it's still > rough-around-the-edges and needs polish. For instance, most of the useful > phobos modules aren't imported by default, and it doesn't yet support > returning string/wstring/dstring - you have to return > char[]/wchar[]/dchar[] > instead and then convert back to string/wstring/dstring (but that shouldn't > be too hard to fix). Also, on Windows it requires a patched version of > rdmd, > which is included with the library ( > http://www.dsource.org/projects/semitwist/browser/trunk/rdmdAlt.d ) but > eval > doesn't yet compile it if it isn't already compiled, and it assumes it's on > the path (I've already solved both of these in the included stbuild > program, > I just need to move the solution over into the general library). My > ultimate > goal with this is to hack up DMD just enough to make it work at > compile-time > (so that *any* arbitrary code can be run at compile-time, albiet more > awkwardly and with much more overhead than ordinary CTFE). > > > >