Hacker News new | ask | show | jobs
by stepik777 3101 days ago
Maybe it is just not a good technology. Just compare it to others:

C++ - memory unsafe, no GC.

Java - memory safe (by default), has GC.

Rust - memory safe (by default), no GC.

D - memory unsafe (by default), has GC.

Authors of Java solved memory safety by adding garbage collector. Authors of Rust managed to make memory safe language without GC while authors of D made a language with GC that is still memory unsafe.

2 comments

It would be a little more accurate to say that in D, memory safety is optional.

If you wrote every method using the @safe flag, you'd be forced to write a memory-safe program. Eg:

    void main() @safe
    {
      int everything = 42;
      int* theAnswer = &a;
      int* earth     = theAnswer * 2; // Won't compile. 
    }
With no flag or @system, safety is not ensured. Using @trusted will allow a function to be called, safe or not, from other safe functions ending the safety. But by design @safe functions can only call other @safe functions.
Yes, D picked the wrong default. That is just a minor annoyance though.

Add "@safe:" to the top of all source files. Check for that in your CI. You are safe now.

The advantage is that you ignore the issue initially. No fighting the type system like in Rust, but easy coding like in Python. Later, when you need your code to be reliable, add annotations and fix problems with compiler guidance.