4 ms·
D doesn't need constexpr. It's much simpler - any function whose value is needed at compile time is evaluated at compile time. For example: int square(int
by WalterBright 9y ago
D doesn't need constexpr. It's much simpler - any function whose value is needed at compile time is evaluated at compile time. For example:
int square(int x) { return x * x; }
const y = square(3); // evaluated at compile time
int bar() {
int[square(2)] array; // evaluated at compile time
return square(3); // evaluated at run time
}
If a value is needed at compile time, and the function cannot be evaluated at compiler time (i.e. it relies on things like global variables not known at compile time) then a compilation error is issued.
There isn't any ambiguity about whether it is evaluated at runtime or compile time - there is no "fall back" to run time if it can't be done at compile time.
- minipci1321 9y agoMaybe 'constexpr' has been added rather to express the intent (for example, for library development)?
- WalterBright 9y agoYou can annotate D functions as 'pure', which would have that effect, but purity is used for other reasons. Evaluating functions at compile time has another wrinkle in D - it is path dependent: int square(int x) { if (x < 100) return x * x; printf("oops\n"); return 0; } const int x = square(3); // works const int y = square(100); // fails at compile time I.e. the path taken through the function has to be pure, not every part of the function.
- danellis 9y agoWhat about values that aren't needed at compile time, but can be so optimized.
- Veedrac 9y agoD still has optimization passes.
- Doxin 9y agoRunning code at compile time in D is not an automatic optimization. Only code in certain constructs are ran at compile time, for example function calls in variable declarations or static if statements. Generally it's not something you even need to think about. This runs foo() at compile time: int bar=foo(); This runs foo() at runtime: int bar; void main(){ bar=foo(); }
- jstimpfle 9y agoI think this might waste resources for some transient results. Sometimes that computation time required for evaluation is negligible, and the result might require more memory than "the function call".
- WalterBright 9y agoIt's been in D for a decade, and has been extremely successful, to the extent of becoming a critical feature. An early example was someone wrote a ray tracer in D that ran and generated output completely at compile time. A more useful example is D's regex package can build the engine at compile time, thereby emitting a custom engine for the regex, instead of at runtime.
- barsonme 9y agoWhile I think it's awesome D can do that (and am a bit jealous the languages I use regularly don't have any real sort of constexpr) what does that _really_ provide? It would seem the startup cost of building a regexp engine would really only matter for, say, command-line programs (i.e., short-lived, run-once programs). For a web application the overhead would be negligible. Again, don't get me wrong, but it just seems to be more of a "neat!" thing than a "we _need_ this for reasons X, Y, and Z."
- WalterBright 9y agoMost regex engines build interpreter bytecode at runtime, and then execute the bytecode with an interpreter. Being able to generate the engine at compile time means the runtime engine is running custom compiled code. So, what it _really_ provides is runtime performance.
- soulbadguy 9y agoHow does D deal with cases where the computation might be plaform dependent (like floating point arth. or bit kung-fu+ endianess) ? An more generally, what happens when a computation "can" be done at compile time , but "needs" (for correctness or ressource ) done at runtime ?
- WalterBright 9y agoThe same way constant folding is done in any compiler. You try to emulate the math on the target. D is quite specific what computations occur at compile time and what at runtime. It's very much up to the user in how he sets it up.
- Doxin 9y agoD doesn't run code at compile time as an optimisation. It only runs code at compile time in certain constructs like variable declarations or static if statements. If you need your code to run at runtime simply stick it in your code where it needs to run.