D Programming Language
Improve this page Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using local clone. Page wiki View or edit the community-maintained wiki page associated with this page.

# Migrating D1 Code to D2

There are many changes to the D programming language that affect migrating source code from D1 to D2. This is an outline of changes to look for and a guide to how to modify the code. See D 2.0 Enhancements for a complete list of changes to the language.

This document is incomplete.

- Core Language
  - New Keywords
  - Global Variables
  - Static Arrays are now Value Types
  - String Literals are Immutable
- Phobos Library

### New Keywords

D2 adds the following keywords: pure, nothrow, shared, and immutable. Any use of them in D1 code must be renamed. Any variable names starting with two underscores should be renamed.

### Global Variables

Global variables are now, by default, stored in thread local storage. To put them back in global storage, use the __gshared storage class:

    int foo = 7; // D1 code
    __gshared int foo = 7; // D2 equivalent

Carefully consider whether or not those variables actually should go into thread local storage, rather than being implicitly shared among all threads.

### Static Arrays are now Value Types

In D1, a static array function parameter is passed by reference, meaning a pointer to the start of the static array is passed as an argument. This means that any changes to the array contents made by the function will be visible to the function's caller. In D2, a copy of the whole static array is passed as an argument. Changes to the array contents by the function are not visible to the function's caller, as it is a separate copy.

To migrate, add the keyword ref to the parameter declaration:

    void foo(int[3] array);      // D1 code
    void foo(ref int[3] array);  // D2 equivalent

### String Literals are Immutable

String literals in D1 have type char[], but in D2 they have type immutable(char)[], which is aliased for convenience under the name string. To migrate usually involves doing a global search replace:

from | to
--- | ---
char[] | string
wchar[] | wstring
dchar[] | dstring

This will take care of most of the issues. For the rest, where mutable strings are desired, there will be some work necessary.

Forums | Comments | Search | Downloads | Home
Copyright © 1999-2013 by Digital Mars ®, All Rights Reserved | Page generated by Ddoc on Tue May 28 10:49:30 2013