-
-
Notifications
You must be signed in to change notification settings - Fork 701
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[experimental] Add NotNull!T #1321
Conversation
One another design by @adamdruppe : #477 |
I might be missing something, but wasn't the point of NotNull in a language to have the determination that something isn't null at compile-time? Having an invariant that throws on null doesn't do much more than trying to dereference a null pointer or access a field on a null class object reference, it's still a runtime error. Perhaps I'm wrong though.. ? |
On Wed, May 29, 2013 at 01:02:49PM -0700, Andrej Mitrovic wrote:
The type system does that. If you ask for a NotNull!T instead of a int* a = something_not_null; *a = 10; // segfault. How did a become null? Gotta look at lots of code NotNull!(int*) a = something_not_null; /* lots of code */ *a = 10; // guaranteed to be fine here If lots of code included an "a = null", that would be a compile error The other thing some languages do is on the assignment, force a null if(a is null) { D can't change a variable's type after it is declared, so we just can't if(a_maybe is null) { and get pretty much the same result. |
Thanks @adamdruppe, that explains it. |
BTW here's the comment i wrote up for the doc before /**
*/ I don't remember why my original pull request with this died. Maybe it was just time, or maybe some flaw, but I definitely remember doing this before and it didn't get committed so I encourage you all to destroy the code. |
According to the pull, more work on it was needed, and then you were too busy to do it, so it got closed. |
@AndrejMitrovic Essentially having I removed the redundancy. |
On Wed, May 29, 2013 at 06:39:39PM -0700, Hara Kenji wrote:
Are unittests redundant too? If all works well, the invariant is |
@adamdruppe OK. Removing invariant was too hasty. I reverted it with detailed comment to explain the purpose why invariant is kept. |
I'd like to explain improved points from #477.
|
I'd like to see this in Phobos. (Personally I prefer
Although NotNull!T assumeNotNull(T)(T t) @system;
NotNull!T notNull(T)(T t) @trusted
{
if (t is null)
throw new NullPointerException;
else
return assumeNotNull(t);
} Using |
Will this be merged into Phobos please? Is it possible to check at compile-time that function may return a null, and get an error if it is so? |
This looks good to me. I like the design, and I see no bugs. It also seems useful, and is something I'd use. While I'm not a fan of the alias this, due to the dangers of implicit pointer dereference, I'll make an exception here, since the whole idea is that said pointer is never null anyways, so the operation should be safe. This works with classes too, right? There's no coverage in the unittests for it. Some extra useage documentation in the form of documented unittest would also be welcome. I suppose that, like "Nullable", a future improvement would be to the ability to specify what the "null state" is, eg: Can I get some more opinion? @andralex or @AndrejMitrovic , maybe?
I'd say it depends on your definition of "may". For example, the compiler could simply error out 100% of the time, if a function returns a pointer or a NotNull, since that "may" be null :/ As for auto p = NotNull!(int*).init; //Actually null. BUT, I think the current design of NotNull is robust enough to give some pretty damn good guarantees, and blow up quite soon if it isn't. |
Maybe we should remove "may" from that statement. Basically I want to mark a function as a function that never returns a null. If it does, I get a compile-time error. Originally I was thinking of writing an UDA @NotNull and mark a function with it, but then Adam reminded me of NotNull!T ... |
I think this deserves a community review and not just a pull request review. NotNull is constantly being discussed after all.. And we don't want to end up in the same situation as with |
Since NotNull has recently been discussed as a language feature (with backing from Andrei & Walter), it's probably best to close this until we have a consensus on the way forward. |
After reading the forum thread "[article] Language Design Deal Breakers", I'd like to propose one
NotNull!T
design.Note that, this is still experimental proposal. If it does not work by current compiler bug, the bug should be fixed first. If this NonNull interface is not good, it should be discussed in here.
Discussion is welcome!