D - Unsafe and doomed

alex burton alexibu.remove at me.com
Tue Jan 7 03:29:17 PST 2014


On Monday, 6 January 2014 at 23:13:14 UTC, Walter Bright wrote:
> On 1/6/2014 3:02 PM, Alex Burton wrote:
>> All the others should result in an exception at some point.
>> Exceptions allow stack unwinding, which allows people to write 
>> code that doesn't
>> leave things in undefined states in the event of an exception.
>
> Hardware exceptions allow for the same thing.

I am not sure what you mean by the above.
To be clear: the below program does not unwind at least on linux. 
Same result using dmd or gdc : Segmentation fault (core dumped).
When I see this from a piece of software I think : ABI problem or 
Amatuer programmer ?

void main()
{
    class Foo
   {
      void bar() {}
   };
   try {
     Foo f;
     f.bar();
   }catch
   {
      writefln("Sorry something went wrong");
   }
}

In my code the vast majority of the references to classes can be 
relied on to point to an instance of the class.
Where it is optional for a reference to be valid, I am happy to 
explicitly state that with a new type like Optional!Foo f or 
Nullable!Foo f;

The phisolsophy of D you have applied in other areas, says design 
is chosen so that code is correct and common mistakes are 
prevented and unwanted inherited features from C are discarded.

In my view it would be consistent to make class references 
difficult to leave or make null by default. I am sure you could 
still cast a null in there if you tried, but the default natural 
language should not do this.

In code where changing this would make a compiler error, in my 
experience the code is fragile and prone to bugs anyway, so 
without a counter example I think the worst that could happen if 
D changed in this way would be people would fix their code and 
probably find some potential bugs they were not aware of.

pointers to structs would still be valuable for interfacing to C 
libraries, and implementing efficient data structures, but the 
high level day to day code of the average user where objects are 
classes by default would benefit from having the compiler prevent 
null class references.



More information about the Digitalmars-d mailing list