3 ms·
Having written a few toy compilers for C, to me D is the most elegant of the C families of languages. For example, consider the versatility of the '.' operator
by optymizer 3y ago
Having written a few toy compilers for C, to me D is the most elegant of the C families of languages.
For example, consider the versatility of the '.' operator. It does pointer dereference, static field access and class/namespace member/method access. In all those cases I want to access "a thing from this other thing". Having 3 operators in a language (., ->, ::) is easier for a compiler writer but it puts more cognitive load on the developer.
Now consider type extensions. As a language designer you could think: "if a developer wishes to add methods to a type Vector that exists in a third party library, then in their project, the developer would use some special syntax" like maybe:
class Vector { void json() { ... } }
and maybe the developer also needs to have it even repeated across a header file and an implementation, but... why? It's an idea, but it's not a law of the Universe. There is a better way.
Instead, the compiler can help you out. Just declare a regular function anywhere like so:
void json(Vector v) { ... }
which is not special in anyway, and then be free to invoke it as:
json(v);
or
v.json();
Why if it isn't our familiar dot operator! It also does type extensions via some syntax sugar? What an elegant solution.
This is just one feature of D, and it's filled with features. Though I don't really use D in my day to day work, it's wonderful to see how the authors thought about hard problems in language design and implemented elegant solutions. I think D deserves more recognition for the things it absolutely hit out of the park as a general purpose programming language.