Re: Read file on compiler time.

2014-05-29 Thread Adam D. Ruppe via Digitalmars-d-learn

string a = import("file.txt");

dmd yourprogram.d -Jlocation_of_file


so for example

dmd yourprogram.d -J.


if file.txt is in the same directory as the .d file.


Re: Read file on compiler time.

2014-05-29 Thread Remo via Digitalmars-d-learn

On Thursday, 29 May 2014 at 20:21:32 UTC, Adam D. Ruppe wrote:

string a = import("file.txt");

dmd yourprogram.d -Jlocation_of_file


so for example

dmd yourprogram.d -J.


if file.txt is in the same directory as the .d file.


Excellent, thank you Adam!

Now another question is it also possible to save/write string at 
compile time?

Probably no...

Any way I like CTFE !


Re: Read file on compiler time.

2014-05-29 Thread bearophile via Digitalmars-d-learn

Remo:


is it also possible to save/write string at compile time?


There is pragma(msg, "...") but it's a little crappy. There are 
plans and a pull request for a good _ctWrite, but it's stalled 
for reasons unknown to me.


Bye,
bearophile


Re: Read file on compiler time.

2014-05-29 Thread Adam D. Ruppe via Digitalmars-d-learn

On Thursday, 29 May 2014 at 20:38:30 UTC, Remo wrote:
Now another question is it also possible to save/write string 
at compile time?


Sort of, use

pragma(msg, "some string");

and it will be printed out when that code is compiled. Important 
that it is when the code is compiled, NOT when the code is 
executed at compile time. So


void foo() { pragma(msg, "here"); }

will say "here" even if you never call foo. So you can't use it 
to print stuff out inside CTFE loops, and you might need to 
shield it with version {} or static if() {} to suppress printing.


But you can use it to print a completed string or something and 
then redirect it to a file in your makefile to do something:


string myfunction() {
   string value;
   foreach(a; ["a", "b"]) value ~= a;
   return value;
}

pragma(msg, myfunction()); // would say "ab"

dmd yourfile.d 2>&1 > foo.txt # redirect all compiler output into 
foo.txt





If you find yourself wanting to print a lot, I say you should 
just use a regualr program that runs normally instead of CTFE. 
Since most the language works the same way, transitioning to and 
from regular execution and compile time execution is easy and 
generally doesn't need you to change most the code.


Re: Read file on compiler time.

2014-05-29 Thread Remo via Digitalmars-d-learn

On Thursday, 29 May 2014 at 20:44:09 UTC, bearophile wrote:

Remo:


is it also possible to save/write string at compile time?


There is pragma(msg, "...") but it's a little crappy. There are 
plans and a pull request for a good _ctWrite, but it's stalled 
for reasons unknown to me.


Bye,
bearophile


Thanks!

Yes I know already about pragma(msg, "...") and use it all the 
time to debug.


Something like _ctWrite would be great even if the name is not 
that great.