Using D Features to Reimplement Inheritance and Polymorphism

Published

Tags: , and

Some months ago I showed how inheritance and polymorphism work in compiled languages by reimplementing them with basic structs and function pointers. I wrote that code in D, but it could be translated directly to plain old C. In this post I’ll show how to take advantage of D’s features to make DIY inheritance a bit more ergonomic to use.

Although I have used these tricks in real code, I’m honestly just writing this because I think it’s neat what D can do, and because it helps explain how high-level features of D can be implemented — using the language itself.

Hacking extern(C++) Classes to Work in betterC

Published

Tags: and

First up, here’s a big disclaimer if the title didn’t warn you enough: this is a hack. It’s just a proof-of-concept for getting extern(C++) classes working with betterC D. Also, DMD keeps getting better quickly, so if you’re reading this post when something more recent than version 2.080 is out, this hack is probably obsolete. Hopefully you’ll find this post interesting anyway if you’re either

If you haven’t read my earlier post about how polymorphism and inheritance work yet, I recommend doing that first.

Xanthe Doesn't Need Linker Hacking Now

Published

Tags: and

I finally got around to dusting off the code for Xanthe to test if it can work without linker hacking, now, too. Short answer: yes. I had to add an implementation of memcmp for the freestanding build, but other than that, all I had to do was throw away the linker hacking steps in the Makefile. Apart from the linker scripts for building the disk images, Xanthe now just compiles normally with -betterC.

Also, the old build was about twice as big as it needed to be because the media files were being packed into the binary twice for no good reason. That doesn’t seem to be a problem any more with the latest dmd.

How Inheritance and Polymorphism Work

Published

Tags: , , and

I’ve promised to write a blog post about the DIY polymorphic classes implementation in Xanthe, the experimental game I once wrote for bare-metal X86. But first, I decided to write a precursor post that explains how polymorphism and inheritance work in the first place.

Look, Ma! betterC Without Linker Hacking!

Published

Tags: and

Version 2.079 of the DMD compiler has just been released with improvements to the -betterC flag. I just gave it a try, and the D code I linked into a C executable with some horrible linker hacking in 2016 now works without any linker hacking at all. There’s still some more stuff to test out when I get around to it, but, hey, here’s some progress:

Making a Compile-time Brainfuck Compiler in D (translation)

Published

Tags: , and

NB: This post is just a translation (with some extra comments by me). Credit goes to the original and the C code generator that inspired it.

Trying Out the New -betterC

Published , Updated

Tags: and

It was less than a year ago that I wrote about linker hacking the runtime out of D code so that it could work as “better C” code, but things have already changed a lot since then. A few days ago Walter Bright announced a new, improved -betterC switch, which can now do a lot of the stuff that needed ugly hacking before.

What is the D Runtime, Anyway?

Published

Tags: and

D’s runtime is a recurring hot topic, but there’s obviously a lot of confusion about what the D runtime even is. I gave a quick explanation during my talk at DConf 2017, but I decided to write a blog post because I’ve seen confusion since then, and because I think blog posts are just a much better format for technical stuff, anyway.

Update

Published

Tags: , , and

Just a quick update because I’ve been too busy to write much recently.

I’m giving a talk at DConf 2017 in Berlin! D’s been growing strongly in the past five years, and DConf’s been growing dramatically since the first one in 2013, so it’s pretty exciting to get involved. No, really. I often give tech talks at no-name events here in Sydney, but I’m half scared I’ll wet my pants on stage with a lineup like this — in my university days, I used to read all the C++ books by Andrei Alexandrescu and Scott Meyer that I could get my hands on.

If you have a DConf ticket, I look foward to seeing you there. If not, then you can look forward to watching the videos :)

Instead of writing a real blog post, I’m dropping a link to this classic about backwards compatibility nightmares, which you might like if you thought the mess that’s x86 BIOS booting was interesting. It’s a chapter from The Old New Thing, a book by Raymond Chen from Microsoft, based on his blog. Raymond Chen has spent a lot of his career making sure new versions of Windows can still run old software, no matter how badly the old software abused APIs and deserved to crash. Most of the technical details belong to the 90s, but there are plenty of morals for software development in the real world today. If you can read that chapter without ever wanting to weep for the industry, you’re stronger than I am.

D for Bare Metal Programming

Published , Updated

Tags: and

This post is somewhat outdated. It’s still relevant, but some of the problems I discussed have already been fixed.

Previously I talked about booting a PC directly to bare metal D and said that Hello World is never a strong test of a programming environment. To get a better feel for what D is really like on bare metal, I wrote Xanthe, a simple, classic-style vertical scrolling shooter game with no dependencies on either the D or C runtime.