Move Constructor Syntax

Salih Dincer salihdb at hotmail.com
Thu Oct 10 18:33:52 UTC 2024


On Thursday, 10 October 2024 at 09:33:21 UTC, Zoadian wrote:
> the fact that you have to add comments shows how bad the syntax 
> actually is.
> imaging having them readable like this instead so one doesn't 
> have to look up the language documentation to figure out what's 
> going on:
> ```
> constructor()
> copy_constructor()
> blit_constructor()
> move_constructor()
> destructor()

I am surprised by suggestions that would break the code just to 
make it look melodious...

Yes, you are suggesting alternative syntaxes for the move 
constructor, which does not officially exist in D. These 
suggestions are quite interesting from a language design 
perspective and could offer different approaches to usage. 
Unfortunately, we are not inventing a new programming language. 
So we can only suggest suggestions that are compatible with the 
existing one. For technical reasons, `this(S)` will not work.

IMO, there's any need to discussion. We have only one option. 
Just do it and let's not waste time:

**Move Constructor Syntax: `=this(S)`**

In the upcoming version of D, we are adding a **move 
constructor** to complement the existing **copy constructor** and 
**destructor**. Just as destructors are invoked with `~this()`, 
move constructors will now be expressed using the intuitive and 
concise syntax `=this(S)`.

The `=this(S)` move constructor enables efficient transfer of 
resources from one object to another without the overhead of 
copying. When an object is moved, its internal resources (such as 
memory pointers) are transferred to the new object, and the 
original object is left in a safe, "moved-from" state, often with 
nullified or reset values.

**Why =this(S)?**

**Symmetry with Destructor:** Just like the destructor `~this()`, 
which frees up resources, the move constructor `=this(S)` 
transfers ownership of resources. This symmetry makes the syntax 
easy to understand and use.

**Efficiency:** Moving objects is more efficient than copying 
because it avoids unnecessary memory allocations or deep copies. 
In the move constructor, resources are transferred directly, 
leaving the original object in a state where its resources can 
safely be discarded.

Don't you want to have what is written above? Let's increase the 
performance as soon as possible because this has gone on for too 
long! I have another question at this stage:

**Question About How Destructors and Move Constructors Work 
Together:**

Since the original object is invalidated, does the destructor 
(`~this()`) know that it has nothing to free when it is called on 
the moved object?

SDB at 79


More information about the Digitalmars-d mailing list