Memory safe in D

bachmeier no at spam.net
Tue Apr 16 15:48:15 UTC 2024


On Tuesday, 16 April 2024 at 07:25:21 UTC, ShowMeTheWay wrote:

> This below is valid C++ code, a bug in C#, but valid code in D 
> (even though it's actually a bug):
>
> A a;
> a.run();
>
> This should not be legal D code. It should produce an error if 
> compiled.
>
> It's not difficult for a compiler to work this one out.

I'm repeating myself, but there's no good argument in favor of 
that compiling. All it gives you is bugs, confusion, and a steep 
learning curve in the name of saving a few keystrokes.

```
import std;
void main() {
   A a;
   writeln(a is null); // true
   B b = null;
   writeln(b is null); // true
   C c = void;
   writeln(c is null); // false, c isn't initialized to null or 
anything else
}
class A {}
class B {}
class C {}
```

`a.run()` is natural. `b.run()` wouldn't make sense even to a new 
programmer. Neither would `c.run()`.


More information about the Digitalmars-d mailing list