Rethinking the Default Class Hierarchy: an Object’s Tale
Every class defined in the D language has
Object as the root ancestor.
Object defines four
opEquals; at first glance, their presence might not
strike you with much, but they are doing more harm than good. Their signatures predate the
introduction of the
@safe function attributes, and also of the
shared type qualifiers. As a consequence, these methods make it difficult to use
Object with qualifiers or in code with properties such as
@safe. We propose
the introduction of a new class,
ProtoObject, as the root class and ancestor of
ProtoObject defines no method and requires the user to implement the desired behaviour
through interfaces: this approach enables the user to opt-in for the behaviour that makes sense
for his class and the design is flexible enough to allow future attributes and language
improvements to be used without breaking code.
We start by having a look at the current design and discuss the shortcomings of
Object. There are two main issues:
- The user has no way of opting out of one of the four methods; Ex. there are classes for which
opCmpdoesn't make sense
- The signatures predate function attributes and qualifiers and this makes it difficult, if not impossible, to work with object in attributed code. Since we sadly can't predict the future and know if and what attributes and qualifiers will be available in the language, this is yet another argument to have a
ProtoObjectwith no methods.
We will discuss how existing code and new code will interact with
ProtoObject in this brave new world.
Eduard is a PhD student and Teaching Assistant at University "Politehnica" of Bucharest. He is a hard working student who enjoys to get his hands dirty
He likes learning new technologies and strengthening his current knowledge. He is passionate about computer science, programming languages, coffee and doing nothing. His previous experience includes distributed systems and parallel programming, operating systems, basic kernel development, open-source software and basic Android programming. He is a Linux fan, a command line addict and a vim enthusiast. He hopes that through his work he will be able to help and improve the D language, which he is becoming so fond of.