Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the use case for D's compile time evaluation

I integrated this feature in my favoriate language OCaml, I know that this is the sexy feature in D, but what's the use case for compile time evaluation except some optimizations? The more the better, the geeker the better :-)

like image 887
bobzhang Avatar asked Jul 12 '12 13:07

bobzhang


1 Answers

Optimization is, of course, a big use case. Things like ctRegex perform better than their runtime compiled regex equivalent (generally). Parser generators are also interesting (see Pegged) and going to be receiving more and more attention. People are just beginning to tap in to what can be done.

You could do something like mixin(import_c("header.h")) to parse and build a D interface file for arbitrary C headers (you would, of course, need to write a parser for C in D to do this).

Extremely fast string formatting can be done since a format string (e.g., "%0.2f") is typically known at compile time. You can read the format string and only generate the code necessary to format, stripping out all sorts of unnecessary sections.

vibe.d actually supports compile time templating. The template file (Jade/HAML based) can contain D code. It reads it and generates a custom block of D code (think of it like "" ~ title ~ "..."). I don't believe the author has done benchmarks but it should be incredibly fast.

You essentially get the benefit of specialized hand-optimized code while staying high level. It's hard to answer your question because we just don't know what it'll be used for. It reminds me of C++ templates. The designers of them did not anticipate the advanced metaprogramming techniques it enabled.

like image 95
eco Avatar answered Oct 01 '22 15:10

eco