Compile-time strings
Literals, const
and immutable
variables in module scope (that aren't initialized in a static constructor), static const
and immutable
variables in function scope, and manifest constants and enum
members, can all be known at compile time. In this section, the focus is specifically on compile-time strings. We're first going to see one more way to initialize them, then we'll see how any compile-time string can be used to generate code.
The import expression
The import
expression is quite different from the import
declaration that pulls module symbols into the current scope. This expression is used to specify any file name for the compiler to read into memory at compile time. The file will be read as text and treated as a string literal, making it possible to assign it to any variable that can be initialized at compile time.
import std.stdio; immutable fileData1 = import("myfile1.txt"); enum fileData2 = import("myfile2.txt"); void main() { writeln(fileData1); writeln(fileData2...