[Bug 178] cc1d locks up when specifying function instead of function pointer in an array

via D.gnu d.gnu at puremagic.com
Mon Apr 6 08:19:35 PDT 2015


http://bugzilla.gdcproject.org/show_bug.cgi?id=178

--- Comment #5 from Ketmar Dark <ketmar at ketmar.no-ip.org> ---
see the part about CTFE here: http://dlang.org/function.html#interpretation

one of the features of D is that it can run code in *compile* *time* (hence the
"CTFE" term — it's "Compile Time Function Evaluation"). compiler can run D code
while compiling your source and use the result to initialise variables.

in your case compiler tries to evaluate `Reset_Handler()` and use it's return
value to init array element. it's a very powerful feature, as you can calculate
various tables without resorting to external "generators". i, for example,
using that to generate parity flag table in my Z80 emulator:

auto genParityTable () {
  ubyte[256] t;
  foreach (immutable f; 0..256) {
    int n, p;
    for (n = f, p = 0; n != 0; n >>= 1) p ^= n&0x01;
    t[f] = (p ? 0 : 1);
  }
  return t;
}
private immutable ubyte[256] tblParity = genParityTable();

here compiler executes `genParityTable()` when it compiling my code and using
it's result to init `tblParity`.

it's vital to understand that `tblParity` is initialized in COMPILE time, there
is no runtime function calls.

the power of D, yeah.

-- 
You are receiving this mail because:
You are watching all bug changes.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/d.gnu/attachments/20150406/024c33aa/attachment-0001.html>


More information about the D.gnu mailing list