Introducing the D Copy Constructor

Razvan Nitu
May 8 @ 11:00


Duration: 50 minutes
Talk type: Presentation
Level: Intermediate
Slides: ODP
Video

Abstract: 

One of the newest D features (not yet released) is the copy constructor. If you want to get a head start on what it can do, how it is better than the postblit and why it was designed this way, then this is the talk for you.

Description

The D version of the C++ copy constructor, historically called “postblit”, offers the advantage of automatic field copying, but comes with the shortcoming that it cannot correctly perform copies from differently qualified source/destination objects. Moreover, the automatic copying of immutable fields is problematic since the object is passed to the user in a cooked state and cannot be further modified. With the evolution of meta-programming the field copying that the postblit does automatically can be easily replaced by a few lines of code, rendering the single advantage of the postblit useless. This is where the new copy constructor steps in: it can create copies from differently qualified objects, therefore being able to initialize immutable objects and it does not present any safety vulnerabilities.

The main topics of the talk will be:

  • Postblit problems
  • Copy constructor as an alternative
  • Syntax
  • Semantics
  • Implicit generation of copy constructors
  • Future (possible) enhancements

The copy constructor is fully implemented and the DIP has been accepted. It is estimated that by the time of this talk, the copy constructor will be available in the master branch of the D compiler.

Speaker Bio:

I am a PhD student doing my thesis on programming languages and I have been contributing to the D compiler for 2.5 years.