Template Metaprogramming Made Easy (Huh?)

bearophile bearophileHUGS at lycos.com
Fri Sep 11 15:26:26 PDT 2009


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 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.
- 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.
- 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).
- 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.
- 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.

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