top | item 43971131

(no title)

ratatoskrt | 9 months ago

> why not compile-time error for reading memory that hasn't been written?

so... like Rust?

discuss

order

Timwi|9 months ago

Curiously, C# does both. It uses compile-time checks to stop you from accessing an uninitialized local and from exiting a struct constructor without initializing all fields; and yet, the CLR (the VM C# compiles to) zero-initializes everything anyway.

munificent|9 months ago

It has to because the analysis to detect that fields are initialized in the constructor body is unsound. Since you have access to `this` inside the constructor, you can call other instance methods which may access fields before they have been initialized.

Java has the same problem.

(Dart, which I work on, does not. In Dart, you really truly can't observe an instance field before it has been initialized.)

mrkeen|9 months ago

This is a pain. I recently switched from Java (and its whole Optional/null mess) to C#. I was initially impressed by its nullable checks, but then I discovered 'default'. Now I gotta check that Guids aren't 0000...? It makes me miss the Java situation.

dontlaugh|9 months ago

That’s likely because p/invoke is quite common.