Template Metaprogramming Made Easy (Huh?)

Jeremie Pelletier jeremiep at gmail.com
Sun Sep 13 16:14:54 PDT 2009


bearophile Wrote:

> Justin Johansson:
> 
> >would you mind saying what salient things there are about D that presumably attracts to the language.  It just helps to know why others are here as one ventures into new territory.<
> 
> That's not an easy question. This is a personal answer, other people will like other sides of D. I like D1/D2 for:
> - I don't think of it as a propetary language, like C#.
> - Sometimes I want the freedom to use memory as I like, with structs, and even unions. If you have large datasets you find that using more than 20 bytes for a number as in Python doesn't help. Values also reduce indirection, this speeds up things. This allows a more efficient usage of memory, and this helps increase cache locality, that increases performance. Unfortunately GC-managed D pointers can't be tagged, so I have to use memory from the C heap for them. And sometimes you need pointers. That's why I'd like D to have more/better ways to increase safety when using pointers (like using memory regions when not in release mode, etc).

I haven't had to use the C heap whatsoever so far in D, could you give me an example of where you need it? In fact, the *only* place I use the C heap is in my garbage collector's internals, for pool structs and mark ranges. I use pointers to GC memory all the time too, there are plenty of algorithms, especially in loops, that can run faster with pointer arithmetic than slices and it's still the fastest way to pass struct references around.

> - I like this newsgroups, I can talk to people, and they sometimes answer my numerous questions. I am learning a lot. Sometimes I receive no answers, but it's acceptable. For its nature this newsgroup attracts some strange people too.
> - I often use Python, it's the language I like more, but for some purposes it's too much slow. And I am tired of writing vectorized code in NumPy and the like. Cython reference count makes me sick and ShedSkin while nice is a failed experiment. D feels like freedom, while sometimes using Python feels like programming with mittens for me.
> - There are some things that I'd like to see in a language, and D being in development still and being not controlled by an ivory tower committee give me the illusion to see some of my ideas realized. So far I haven't influenced a lot the development of D. On the other hand if everyone can influence a lot the language the result may be a patchwork. So some dynamic compromise has to be found every day.

I also like this community driven model, but this forum has more people submitting ideas than people able to implement them on time, I'm pretty sure the TODO list is rather huge at this time :) I for one much prefer D development the way it is now than the "working group" model used by the W3C or Khronos for example.

The public bugzilla is really nice too, once you get used to it, one of the issues I submitted got fixed in 2.032, I've also sent maybe 3 or 4 patches to the compiler source in other issues so far too, hopefully they'll be used in 2.033!

> - D looks a lot like C, yet in D I can write code several times faster than C. Sometimes 5-10 times faster. This is an enormous difference.

Indeed, and sometimes it's way faster than that. I've ported many C headers to D and I'm always amazed at how many things I can throw out, just the DirectX headers were at least 50% smaller in D and MUCH easier to read. Such simplicity is also reflected in the compiler by having quite a lot less tokens and parse nodes to create and analyze.

I must admit however that I sometimes miss the C preprocessor, or at least wish mixins had a syntax closer to that used by the C preprocessor. But it's a good idea to keep D without a preprocessor, its much better for everything to have a scope.

> - I am quite sensitive to syntax noise, boilerplate code. I like elegance, clarity in semantics, I hate corner cases, yet I want a language that's efficient, readable, and the less bug-prone as possible. C++ looks too much complex for me. D1 is simple enough for me, and D2 is getting a bit too much complex. I may appreciate the idea of a D 1.5 language that fixes some holes and limits of D1 while keeping language simple enough (struct constructors, and few other things. Such things don't significantly increase the difficulty in using the language).

C++ isn't anymore complex than D2, its syntax just isn't as elegant. Other than multiple inheritance which is partially solved through object composition, I can't think of many features C++ has over D2. I can name quite a few features D2 has over C++ :)

What I like about D is that while its elegant, it still allows for dirty work to be done when you need it. You don't need to code your application core in C and your application behavior in a scripting language on top of the C core. D allows you to write it all in one language with the same productivity, if not better productivity for not having to write the abstraction layer between C and scripting.

> - I like how D doesn't totally ignore safety as C does, in D sometimes the default way is the safer one, and the unsafe way is used only where you ask for it.  I'd like to see more safeties added to D, like optional run-time and compile-time integral overflow tests, some pointer safety, better template error messages (template constraints help some in such regard), stack traces, less compiler bugs, safer casts (in C# you need explicit casts to convert double => float), a safer printf, some optional annotations inspired by Plint (a lint program) to give more semantics to the compiler, that can be used to both speed up code and avoid bugs. There's lot that can be done in this regard. And release-mode performance can be usually kept unchanged.

Stack traces is a feature for the runtime, I made one for mine, which shows a dialog window with the stack trace, current registers values, loaded modules and whatnot. Took me almost a week to piece together my CodeView reader, I still need a DWARF reader for linux. I'm gonna try and implement it in druntime and submit the patch to bugzilla.

> - I like how templates allow me to do some things that are doable only in less common languages like Lisp and Haskell, or in dynamic languages.
> - To use D I don't need to fill forms, receive emails, pay, install with an installer, or use an IDE. The compiler is free, you just need to download a zip, and recently such zip is well organized too inside. I uncompress the zip, seth a path or two and I am already able to use it.
> - The LDC compiler on Linux produces binaries that are efficient almost as C++ and sometimes more. Someday LDC will be available on Windows too. LDC developers are good people, they fix things very quickly (often in 24 hours), and they don't ignore user requests. For example in LDC the == among associative arrays now works. LDC developers are almost as serious people as LLVM devs, but no one gets paid for LDC (while the head of LLVM is paid by Apple).
> - D contains many tricks and handy features that save lot of time and make programming shorter and simpler. Some of such features are half-unfinished (some GC/GC-pointers semantics, module system, unittest system, contract programming, and several other things) and Walter doesn't seem willing to finish them soon, but having them partially unfinished is better than not having them.

There are hundreds of things on Walter's list, he can't magically do them all overnight :)

> 
> So in summary I like the freedom D gives me in using memory, and the freedom to program in the style I see most fit for a program, its work-in-progress nature that gives the illusion to be able to influence it in good ways, its simple enough nature, its handy features, its clear and short enough syntax (even if there are ways to improve it still). I like people in the D community, because they sometimes understand what's the way to improve the language.
> 
> But probably the top two reasons for me are that it allows me to write fast programs and at the same time it's not a very bug-prone language. Helping me avoid bugs saves a ton of my programming time. Saves my time both when I program and when I run the program. Compared to this all other things are secondary.
> 
> Bye,
> bearophile



More information about the Digitalmars-d mailing list