I've heard D can execute arbitrary user code at compile time. Is this true? Could someone give an example (particularly when compilation never terminates)? What this feature is for? Also if that's so is there a way to disable this with some compiler option (compiler is dmd)?
Asked
Active
Viewed 182 times
1 Answers
5
Yes, it's partially true. D has compile time function execution, i.e. it has the ability of executing functions at compile time. But such functions are required to be both portable and free of side effects.
You will find an in-depth explanation in the official documentation of the D language.

Roberto Reale
- 4,247
- 1
- 17
- 21
-
3I would add too that while you can put the compiler into an infinite loop if you wanted to, it can't do anything more malicious than that; it can't overwrite files or send spam on the network or anything like that. In your own program, compile time execution is opt-in so you can just not use it if you don't want to, and in other people's programs, I'd just say if the compile is taking unreasonably long just hit ctrl+c to forcibly stop it. – Adam D. Ruppe Apr 18 '14 at 14:47
-
@AdamD.Ruppe it could make the executable unreasonably large by bloat – ratchet freak Apr 19 '14 at 03:02
-
@ratchetfreak sometimes "bloated" programs are more useful than super-compact stuff which does everything on runtime, and besides, it can be controlled by the (library) programmer, so its a super useful tool – Quonux Apr 28 '14 at 08:19