On Tuesday, 23 February 2016 at 22:03:32 UTC, Xinok wrote:
On Tuesday, 23 February 2016 at 19:43:43 UTC, rsw0x wrote:
...
How does this differ from the example I gave where the branch
is only taken if the pointer is non-null?
D doesn't prevent you from dereferencing a null pointer whereas
these scenarios should be impossible in Kotlin as well as Rust.
Case and point, this code compiles without issue but crashes at
runtime:
int* foo()
{
return null;
}
void main()
{
if(int* ptr = new int)
{
ptr = foo(); // Whoops...
*ptr = 35; // Crash
}
}
In D, pointers and reference types can spontaneously become
null under almost any context. That's the difference.
The idea is to match non-null pointers, this is how you do it:
int* foo()
{
return null;
}
void main()
{
if(int* ptr = foo())
{
*ptr = 35; // no crash
}
}