Phobos - breaking existing code

bearophile via Digitalmars-d digitalmars-d at puremagic.com
Sun Nov 30 15:16:12 PST 2014


Walter Bright:

> I'm aware that D2 offers many opportunities to improve the code 
> :-)

One problem I am seeing is that there is a large distance from 
the bare-bones code D2 requires you to write, and the code that I 
now regard as good D2 code. And the D2 compiler doesn't require 
or pushes you much toward the better code.

You can see that even for in the smallest things, like:

foreach (i; 0 .. 10) {
     writeln(i);
     i++;
     writeln(i);
}

I suggested to make that variable i an immutable by default. 
Currently it's a mutable and nearly no one bothers to write the 
correct and safer code:

foreach (immutable i; 0 .. 10) {
     writeln(i);
     writeln(i + 1);
}


The moral of this story is that a language needs to be designed 
to have by default the best&safe idioms (but avoiding excessive 
amounts of typing as Ada). This is a hard balance to find.

Here you can see two examples of code that show the difference 
from the C-style code that compiles with D2, and the code you can 
write in D2 only if you push yourself a lot to express more 
invariants and more precise typing:

http://rosettacode.org/wiki/Hidato#D

(The two programs are similar but they don't do exactly the same 
things. The second program is longer also because it does a 
little more).
Most of the D code you see in the wild is the C-like code of the 
first kind.

This problem is present even in Ada code or F# code, but in F# 
the "basic" style of coding is rather safer than D written in the 
bare bones C-style, and it's rather crisp.

Bye,
bearophile


More information about the Digitalmars-d mailing list