> A difference I am uncertain about is if there's any D equivalent for Zig having types being expressions. You can, for example, calculate what the return type should be given a type of an argument.
This is done in D using templates. For example, to turn a type T into a type T star:
template toPtr(T) { alias toPtr = T*; } // define template
toPtr!int p; // instantiate template
pragma(msg, "the type of p is: ", typeof(p));
The compiler will deduce the correct return type for a function by specifying auto* as the return type:
auto toPtr(int i) { return cast(float)i; } // returns float
For conditional compilation at compile time, D has static if:
enum x = square(3); // evaluated at compile time
static if (x == 4)
int j;
else
double j;
auto k = k;
Note that the static if* does not introduce a new scope, so conditional declarations will work.
The version is similar, but is intended for module-wide versions, such as:
version (OSX)
{ stuff for OSX }
else version (Win64)
{ stuff for Windows 64 }
else
static assert(0, "unsupported OS");
Compile time execution is triggered wherever a const-expression is required. A keyword would be redundant.
D's mixins are for generating code, which is D's answer to general purpose text macros. Running code at compile time enables those strings to be generated. The mixins and compile time execution are not the same feature. For a trivial example:
string cat(string x, string y) { return x ~ "," ~ y; }
string s = mixin(cat("hello", "betty")); // runs cat at compile time
writeln(s); // prints: hello,betty
I appreciate you taking the time to give examples in D. People are often under the mistaken impression that Zig's compile time is revolutionary, from how it is excessively hyped, but are failing to realize that many other languages have similar or users can get similar results by doing things differently, because languages have different philosophies and design strategies.
For example, the creator of Odin, has stated in the past he rather come up with optimal solutions without metaprogramming, despite enthusiasts trying to pressure him to add such features into that language.
This is done in D using templates. For example, to turn a type T into a type T star:
The compiler will deduce the correct return type for a function by specifying auto* as the return type: For conditional compilation at compile time, D has static if: Note that the static if* does not introduce a new scope, so conditional declarations will work.The version is similar, but is intended for module-wide versions, such as:
Compile time execution is triggered wherever a const-expression is required. A keyword would be redundant.D's mixins are for generating code, which is D's answer to general purpose text macros. Running code at compile time enables those strings to be generated. The mixins and compile time execution are not the same feature. For a trivial example:
I'll be happy to answer any further questions