| 1 | = Reading Files at Compile Time = |
---|
| 2 | |
---|
| 3 | == Description == |
---|
| 4 | |
---|
| 5 | D lets you do a lot code execution during compile time, also known as Compile Time Function Evaluation (CTFE). Sometimes the information you want to work with at compile time is located in a file, you can access the content by using import("filename"); which returns a character array. |
---|
| 6 | |
---|
| 7 | == Example == |
---|
| 8 | |
---|
| 9 | File: ctfi.txt |
---|
| 10 | {{{ |
---|
| 11 | Hello World |
---|
| 12 | }}} |
---|
| 13 | |
---|
| 14 | File: ctfi.d |
---|
| 15 | {{{ |
---|
| 16 | #!d |
---|
| 17 | import std.stdio; |
---|
| 18 | |
---|
| 19 | void main() { |
---|
| 20 | const foo = import("ctfi.txt"); |
---|
| 21 | |
---|
| 22 | writefln(foo); |
---|
| 23 | } |
---|
| 24 | }}} |
---|
| 25 | |
---|
| 26 | If you try to compile this with $ dmd ctfi.d you will receive the error |
---|
| 27 | |
---|
| 28 | {{{ctfi.d(6): Error: need -Jpath switch to import text file ctfi.txt}}} |
---|
| 29 | |
---|
| 30 | This is the path for file imports that are not libraries. Use the following for compilation: |
---|
| 31 | |
---|
| 32 | Compile: |
---|
| 33 | {{{ |
---|
| 34 | dmd -J. ctfi.d |
---|
| 35 | }}} |