On Fri, 16 Jan 2009 10:30:53 +0300, Qian Xu <quian...@stud.tu-ilmenau.de> wrote:

When shall I use some_var.dup and when not?
Is there any guidlines?

--Qiansua

.dup is typical way to makes a copy of variable:

char[] greetings = "Hello, World!".dup;

You usually do this when you want to modify variable that you are not allowed 
modify in-place.
Consider the following example:

auto greetings = "Hello, World!";

The "Hello, World!" string is not allowed to be modified, because it could be 
shared throughot the project and will be most probably put in a read-only memory causing 
segfault at modification.

But it you need to have a modified version of this this, you create its copy 
(duplication, or 'dup' for short) and make whatever changes you want to it:


char[] copy = greetings.dup;
copy[0] = "T"; // copy -> "Tello, World!"

.dup may be applied to arrays (including strings and maps aka associative 
arrays).

You should write your own .dup method for your classes (deciding whether you 
want a deep copy or not), and it is not needed for struct, because assignment 
does everything for you (unless you want deep copy).

Hope that helps.

Reply via email to