D1 -> D2
Steven Schveighoffer
schveiguy at yahoo.com
Thu Nov 18 04:34:01 PST 2010
On Thu, 18 Nov 2010 05:51:32 -0500, Fawzi Mohamed <fawzi at gmx.ch> wrote:
> Is there any "porting" guide around in a wiki?
> If not a page where to share the best tricks would be nice "D1->D2
> conversion tricks"?
>
> In the short term I don't think that going D2 only is really an option
> for me, so how feasible it is to keep the code base compatible to both
> D1 and D2?
>
> I know that one can define some templates (for example Const(T),....),
> and maybe use mixins, but how much uglier does the code become as result?
> I choose D to have cleaner code, I am not interested in loosing all that
> just to be D1 and D2, then I prefer to wait, and convert everything at
> once.
I have had two experiences moving code from D1 to D2. First, I tried to
port Tango to D2, and found that backwards compatibility is not possible.
So I was simply reorganizing the files. The hugest problem for going from
D1 to D2 is string literals. Every place you use char[] has to be
switched to const(char)[] or string. I came up with a set of rules for
this:
1. if the 99% common case for string arguments will be string literals,
then use string -- you never have to worry about duping.
2. if you intend to keep a copy of the string, but you aren't sure whether
the string to be passed in will have mutable aliases, use const(char)[]
and dup.
3. if you don't intend to keep a copy of the string, use const(char)[] to
have the most flexibility.
4. if you intend on returning a portion of the string, well, currently
you're SOL, but eventually inout will come into play here.
For example, Tango's log package fell under rules 1 and 3 (most of the
time you are creating log objects with string literals, and logging a
message does not save the message anywhere).
Many many cases fell under rule 4, which is one of the main reasons I gave
up (inout still isn't ready).
The second experience I had was porting dcollections to D2. Since
dcollections has not many places for string literals, it was much easier
to port. I don't think I made it backwards compatible, but it's very very
similar to the original in terms of constancy. I've added a few features
that only really make sense in D2 (ranges for one).
Porting dcollections was quite easy, but with my experience trying to port
Tango, I never tried to make it backwards compatible.
My recommendation -- when you are ready, switch wholly to D2. Don't
bother with compatibility, it's just not possible.
-Steve
More information about the Digitalmars-d
mailing list