Read files at compile time
Robert Fraser
fraserofthenight at gmail.com
Thu May 31 01:39:22 PDT 2007
Hi Henrik,
Check out http://www.digitalmars.com/d/expression.html#ImportExpression , which can read files in as compile-time string literals. You'd then need to write a CTFE-compliant string-splitting function and mix that back in. So, you'd have:
mixin(ctfe_split("archetypes", import("file.txt")));
Which should become something like:
char[][3] archetypes = [ "foo", "bar", "baz", ];
Writing the function itself shouldn't be _too_ hard, but there are a number of constructs which make it unable to be executed at compile-time, so ensure it has no side effects.
Regards,
Fraser
Henrik Wrote:
> Hello!
>
> I have a question or two regarding what is allowed to be executed at compile time and not. Consider the following:
>
> <code>
> import std.stdio;
> import std.file;
> import std.string;
>
>
> char[][] loadArchetypes( char[] fileName )
> {
> char[][] archetypes = splitlines( cast(char[])read( fileName ) );
>
> return archetypes;
> }
>
>
> void main()
> {
> /// Load archetypes
> const char[][] archetypes = loadArchetypes( "archetypes.txt" );
>
> foreach( archetype; archetypes )
> {
> writefln( archetype );
> }
> }
>
> </code>
>
>
> I suspect that I am not allowed to do this for some reason. I get the very unhelpful error:
> Compiling: fileTest.d
> Error: variable useWfuncs is used before initialization
> Process terminated with status 1 (0 minutes, 0 seconds)
> 0 errors, 0 warnings
>
>
> Is it possible to read a file at compile-time in the manner I am trying to do? What does the error message mean?
>
>
> Yours most confusedly,
>
> Henrik
More information about the Digitalmars-d-learn
mailing list