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.
struct Vector2 { int a ; int b ; /++ Automatically generates this constructor: this(int a = int.init, int b = int.init) { this.a = a; this.b = b; } ++/ } void main () { 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
... continue reading