10 features of D that I love This is a beginner-friendly post exploring some of my favourite parts of the D programming language, ranging from smaller quality of life stuff, to more major features. I won’t talk much about D’s metaprogramming in this post as that topic basically requires its own dedicated feature list, but I still want to mention that D’s metaprogramming is world class - allowing a level of flexibility & modelling power that few statically compiled languages are able to rival. I’ll be providing some minimal code snippets to demonstrate each feature, but this is by no means an in depth technical post, but more of an easy to read “huh, that’s neat/absolutely abhorrent!” sort of deal. Summary Feature - Automatic constructors If you define a struct (by-value object) without an explicit constructor, the compiler will automatically generate one for you based on the lexical order of the struct’s fields. /++ Automatically generates this constructor: this(int a = int.init, int b = int.init) const noParams = Vector2(); const oneParam = Vector2(20); // Sets .a to `20` const twoParams = Vector2(20, 40); // Sets .a to `20` and .b to `40` Very handy for Plain Old Data types, especially with the semi-recent support for named parameters. Feature - Design by contract D supports contract programming which allows functions to define: “in” assertions to confirm that the function’s parameters are valid. “out” assertions to confirm that the function’s return value is in a valid state. Additionally you can attach “invariants” onto structs and classes. Invariants are functions that run at the start and end of every public member function, and can be used to ensure that the type is always in a valid state. Let’s start off with a contrived example of invariants: // Arbitrary function syntax assert(_lower >= 0, "_lower must not be negative"); assert(_upper >= 0, "_upper must not be negative"); // Short hand syntax, translates to a single `assert()`. invariant(_upper >= _lower,...
First seen: 2025-07-02 21:58
Last seen: 2025-07-03 07:01