Menu

- Learn
- Documentation
  - Language Reference
  - Library Reference
  - Command-line Reference
  - Feature Overview
  - Articles
- Packages
- Community
  - Blog
  - Orgs using D
  - Twitter
  - Forums
  - IRC
  - Wiki
  - GitHub
  - Issues
  - Foundation
  - Donate
- Resources
  - Books
  - Tutorials
  - Tools
  - Editors
  - IDEs
  - Visual D
  - Acknowledgments
  - D Style
  - Glossary
  - Sitemap

Search
Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
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 a local clone.

# 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.

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.

Copyright © 1999-2017 by the D Language Foundation | Page generated by Ddoc on Thu Mar 9 17:49:30 2017