Which D features to emphasize for academic review article

Walter Bright newshound2 at digitalmars.com
Fri Aug 10 15:01:15 PDT 2012


On 8/10/2012 1:38 AM, F i L wrote:
> Walter Bright wrote:
>> 3. Floating point values are default initialized to NaN.
>
> This isn't a good feature, IMO. C# handles this much more conveniently with just
> as much optimization/debugging benefit (arguably more so, because it catches NaN
> issues at compile-time). In C#:
>
>      class Foo
>      {
>          float x; // defaults to 0.0f
>
>          void bar()
>          {
>              float y; // doesn't default
>              y ++; // ERROR: use of unassigned local
>
>              float z = 0.0f;
>              z ++; // OKAY
>          }
>      }
>
> This is the same behavior for any local variable,

It catches only a subset of these at compile time. I can craft any number of 
ways of getting it to miss diagnosing it. Consider this one:

     float z;
     if (condition1)
          z = 5;
     ... lotsa code ...
     if (condition2)
          z++;

To diagnose this correctly, the static analyzer would have to determine that 
condition1 produces the same result as condition2, or not. This is impossible to 
prove. So the static analyzer either gives up and lets it pass, or issues an 
incorrect diagnostic. So our intrepid programmer is forced to write:

     float z = 0;
     if (condition1)
          z = 5;
     ... lotsa code ...
     if (condition2)
          z++;

Now, as it may turn out, for your algorithm the value "0" is an out-of-range, 
incorrect value. Not a problem as it is a dead assignment, right?

But then the maintenance programmer comes along and changes condition1 so it is 
not always the same as condition2, and now the z++ sees the invalid "0" value 
sometimes, and a silent bug is introduced.

This bug will not remain undetected with the default NaN initialization.


> so where in D you need to
> explicitly set variables to 'void' to avoid assignment costs,

This is incorrect, as the optimizer is perfectly capable of removing dead 
assignments like:

    f = nan;
    f = 0.0f;

The first assignment is optimized away.

 > I just think D's has other, much better advertising points that this one.

Whether you agree with it being a good feature or not, it is a feature unique to 
D and merits discussion when talking about D's suitability for numerical 
programming.





More information about the Digitalmars-d mailing list