Programming fun

Where programming and development is concerned it’s always good to get some time to just play, to mess around with new tools, languages or libraries just for the fun of it to see what you can do. I don’t really do this often enough, but the last few days were an exception. Here’s what I got up to …

C, C++, D …

I can’t remember when exactly I discovered the D programming language, but if my posts on the project mailing lists are anything to go by it must have been in early 2010. This overlapped very nicely with the period in which I became properly enthusiastic and confident with C++, and I was delighted by D’s much more elegant and intuitive syntax compared to the older language.

I’ve played with it on-off ever since then, but never with the commitment that the language really deserves. There were various reasons for that; one of the major ones was that the only up-to-date compiler was the partially-proprietary reference implementation, with the GCC and LLVM-based ones lagging behind.

However … the last release of Ubuntu (11.10) saw the GCC 4.6 based GDC compiler included, and the upcoming LTS release (12.04) has promoted this to being the default GDC install. In fact this is a straight import from Debian, meaning that all upcoming Debian-derived distros are available. So, it seemed an opportune moment to dive back in.

The results are quite nice. Back when I was first trying out D there was a big speed gap with C++ which the latest GDC seems to have closed. The GDC dev community is full of beans and doing excellent work. With any luck the LLVM-based LDC compiler will catch up soon and also be available in distros.

My explorations in the next weeks will probably be to do with things like template mixins and other ways in which D differs from C++ in its approach to class templates, inheritance, polymorphism and so on. I’ll also be looking into how easy it is to use D with C and C++ libraries, surely essential at this point when there is not a widespread range of D software available in Linux distros.

Clang!!!!

Looking into D made me think of LDC, which in turn reawakened my casual curiosity in the LLVM compiler project. I’ve never really worked with any of these tools beyond LDC, so thought I’d have a play with Clang, the LLVM-based C++ compiler.

A couple of tweaks to a Makefile later and … oh dear oh dear! Compiler error messages abounded. But once I’d got past the ‘But it works fine with g++!’ panic, they turned out to be useful, informative, and easy to resolve. The code in question was a policy class-based program with a ton of template metaprogramming, and it turns out that Clang/LLVM is much stricter than GCC when it comes to certain potential ambiguities in C++ templates.

In fact, the error messages gave precise instructions for resolving the problem — in this case, to insert ‘this->’ keywords in front of various function calls. Compilation itself was noticeably faster than g++ and produced code that, while slower, was not so much so as to be unpleasant.

On this basis, I’m quite tempted to switch to Clang as my development compiler, using these nice features to ensure code correctness and to avoid long compile times, and pull out g++ only when building a finished program.