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
- Phobos Library
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 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.
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 array); // D1 code void foo(ref int array); // D2 equivalent
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:
This will take care of most of the issues. For the rest, where mutable strings are desired, there will be some work necessary.