It seems pure ain't so pure after all

Jonathan M Davis jmdavisProg at gmx.com
Mon Oct 1 11:36:32 PDT 2012


On Monday, October 01, 2012 19:46:16 Tommi wrote:
> On Monday, 1 October 2012 at 08:04:49 UTC, Jonathan M Davis wrote:
> > And you _can't_ determine ahead of time which functions can be
> > safely executed at compile time either, because that's an
> > instance of the halting problem.
> 
> I don't understand (I did read what "halting problem" means just
> now, but I still don't understand). If there was no __ctfe
> variable, and thus a guarantee that all functions do the same
> thing at compile-time and run-time,

__ctfe exists purely so that you can provide an alternate implementation which 
works at compile time when the normal implementation doesn't (since CTFE _is_ 
more restrictive in what it allows than running a function at runtime is).

> couldn't the compiler just
> try aggressively to execute all function calls at compile-time?

So, the compiler is supposed to run every function at compile time and then 
back up if it a function doesn't work? It can't even always know for sure when 
a function doesn't work (e.g. infinite loop). This would absolutely _tank_ 
compilation times. Most functions _can't_ be run at compile time simply 
because of how they're called, and the compiler can't necessarily know that 
unless it does full flow analysis if not outright _runs_ every function, which 
would be _incredibly_ expensive.

> If not a compiler flag, then it could be a function attribute.
> Just like c++ function attribute constexpr, which guarantees that
> the function executes at compile time given you provide it with
> compile-time evaluable arguments (and there are limitations to
> what the function can do). Why wouldn't this attribute be
> possible with D?

CTFE was specifically designed with the idea that you would not need to mark 
functions as CTFEable. You can call _any_ function at compile time. Some will 
fail, because they're doing things that CTFE won't allow, but that's quickly 
caught, because it happens at compile time. It completely avoids needing to 
mark functions as CTFEable all over the place (which would generally mean that 
functions wouldn't be CTFEable, because people would frequently not mark them 
as CTFEable). constexpr is in direct conflict with that design goal.

And if you want a function to be executed at compile time, you assign its 
result to an enum or static variable. Done. It's easy and straightforward. I 
really don't understand why this is an issue at all.

- Jonathan M Davis


More information about the Digitalmars-d mailing list