3 ms·
D had it 17 years ago! D features steadily move into other languages. > Here the comptime keyword indicates that the block it precedes will run during the comp
by WalterBright 2y ago
D had it 17 years ago! D features steadily move into other languages.
> Here the comptime keyword indicates that the block it precedes will run during the compile.
D doesn't use a keyword to trigger it. What triggers it is being a "const expression". Naturally, const expressions must be evaluatable at compile time. For example:
int sum(int a, int b) => a + b;
void test()
{
int s = sum(3, 4); // runs at run time
enum e = sum(3, 4); // runs at compile time
}
By avoiding use of non-constant globals, I/O and calling system functions like malloc(), quite a large percentage of functions can be run at compile time without any changes.
Even memory can be allocated with it (using D's automatic memory management).
- WalterBright 2y agoHere's one of my favorite uses for it. I used to write a separate program to generate static tables. With compile time function execution, this was no longer necessary. Here's an example: __gshared uint[256] tytab = tytab_init; extern (D) private enum tytab_init = () { uint[256] tab; foreach (i; TXptr) { tab[i] |= TYFLptr; } foreach (i; TXptr_nflat) { tab[i] |= TYFLptr; } foreach (i; TXreal) { tab[i] |= TYFLreal; } /* more lines removed for brevity */ return tab; } (); The initializer for the array `tytab` is returned by a lambda that computes the array and then returns it. A link to the full glory of it: https://github.com/dlang/dmd/blob/master/compiler/src/dmd/backend/var.d#L184 https://github.com/dlang/dmd/blob/master/compiler/src/dmd/ba... Another common use for CTFE is to use it to create a DSL.
- optymizer 2y agoWalter, I'll take any chance I can get to say: thank you for creating D! One thing I was wondering about is the limits of compile time execution. How does the D compiler ensure correctness if the machine the compiler runs on is different from the machine the program will execute on? For example, how does the compiler know that "int s = sum(100000, 1000000)" is the same value on every x86 machine? I'm thinking there could be subtle differences between generations of CPU, how can a compiler guarantee that a computation on the host machine will result in the same value on the target machine in practice, or is it assuming that host and target are sufficiently similar, as long as the architecture matches? (which is fine, I'm wondering as to what approaches exist)
- WalterBright 2y ago> thank you for creating D! My pleasure! > is the same value on every x86 machine? It's the same value on all machines, because integer types are fixed size (not implementation dependent) and 2's complement arithmetic is mandated. Floating point results can vary, however, due to different orders in which constants are evaluated. The x87, for example, evaluates to a higher precision and then rounds it only when writing to memory.
- chainingsolid 2y agoI'll second thanking you for making D. I still haven't found a language with more compile time capabilities that I can/would actually use. So I'm still using D. Any thoughts on adding something like Zig's setFloatMode(strict)? I have a project idea or 2 where for some of the computation I need determinism then performance. But very much still need the performance floating point can provide.
- WalterBright 2y agoThanks for the kind words! Your best bet for floating point determinism is to stick with doubles. Then, in 64 bit code, the double math will be done with the XMM registers and instructions, which will stick with 64 bit arithmetic.
- WalterBright 2y agoD's ImportC also can do CTFE with C code! int sum(int a, int b) { return a + b; } _Static_assert(sum(3, 4) == 7, "look ma, check at compile time!"); Why doesn't the C Standard add this? It works great!
- flohofwoe 2y agoTbf, Zig allows that too (calling the same function in a runtime and comptime context): fn square(num: i32) i32 { return num * num; } pub fn main() void { _ = square(2); _ = comptime square(3); } ...and the comptime invocation will produce a compile error if anything isn't comptime-compatible (which IMHO is an important feature, because it rings the alarm bells if code that's expected to run at comptime accidentially moves into runtime because some input args have changed from comptime- to runtime-evaluated).
- WalterBright 2y agoD does not need the extra keyword. The extra keyword is the same fault that C++ `constexpr` has. It's simply not necessary. The trigger for D is `constant-expression` in the grammar. We have a lot of D users that come from C++, and not one of them has ever asked for `constexpr` that I've heard.
- flohofwoe 2y agoI see the keyword as a feature, not a crutch ;) It guarantees that this area of code will always be evaluated at comptime, and otherwise fail to compile. Compilers (or rather their optimizer passes I guess) already try to fold a lot of code into a constant if it can be evaluated at compile time, but it will silently fall back to runtime evaluation when a variable slips into the code later. With a keyword I can at least say "please fail when that happens".
- WalterBright 2y agoD guarantees that code that is an implementation of a constant-expression is evaluated at compile time. There really is no purpose to adding a keyword.
- drjeats 2y agoIn the most common cases, Zig does exactly what D does here as well. Constant expressions are folded at compile time like any reasonable systems language. It's akin to writing C++ where you slap constexpr on everything. Zig agrees with D that constexpr is silly :) The one thing is Zig doesn't have D's notion of function purity, so I suspect there are cases where D could either infer that an expression is const where Zig can't, or at the very least D's compiler could do it faster. Not to dismiss D's contributions to systems programming languages, of course. Clearly a ton of inspiration of being pulled from y'all's work.