NonNull template
Meta
jared771 at gmail.com
Fri Apr 18 06:25:22 UTC 2025
On Thursday, 17 April 2025 at 22:12:22 UTC, Jonathan M Davis
wrote:
> On Thursday, April 17, 2025 11:36:49 AM MDT Dave P. via
> Digitalmars-d wrote:
>> On Thursday, 17 April 2025 at 16:39:28 UTC, Walter Bright
>> wrote:
>> First to support classes, you’d have to make a slight change
>> ```d
>> struct NonNull(T)
>> {
>> T p;
>> T ptr() { return p; }
>> alias this = ptr;
>> }
>> ```
>>
>> It works, but the syntax/defaults is backwards. Why does the
>> unusual case of a nullable pointer get the nice syntax while
>> the common case gets the `NonNull!(int*)` syntax? Who is going
>> to write that all over their code?
>
> That being said, I honestly think that the concern over null
> pointers is completely overblown. I can't even remember the
> last time that I encountered one being dereferenced. And when I
> have, it's usually because I used a class and forgot to
> initialize it, which blows up very quickly in testing rather
> than it being a random bug that occurs during execution.
When you work on a team with less skilled/meticulous teammates on
high performance, highly parallel software, you realize how
amazing it would be to have a language where pointers are
guaranteed to be non-null by default. I spent so much time fixing
random annoying NPEs that other people wrote that pop up randomly
in our test cluster.
This feature is not for people like you. It is to protect your
sanity from the terrible code that people who are not as
principled will inevitably write. This is an absolute no-brainer
IMO, and it's one thing that is really great about Rust.
More information about the Digitalmars-d
mailing list