-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
ability to compare a pointer with a nullable pointer #658
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
Comments
See #386 (comment) |
I propose comparisons work with var maybe: ?bool = null;
if (maybe != false) {
// ...
} (Furthermore, the error message you get is pretty misleading, The motivation for this issue is that the current workaround is nominally suboptimal performance due to having two conditional branches instead of just one: if (!(maybe != null and (maybe ?? unreachable) == false)) {
// ...
}
// or
if (!if (maybe) |value| value == false else false) {
// ...
} For nullable types that are implemented to fit into a single word (like nullable pointers and bools), the optimal assembly would be a single comparison instruction, which looks more like |
This looks reasonable to me: var maybe: ?bool = null;
if (maybe ?? true) {
} The performance argument isn't valid. Currently, the above code generates struct {
payload: bool,
non_null: bool,
} And so doing the comparison is 2 comparisons. If we were to represent the data in a different way, such as this: enum {
Null,
False,
True,
} Then the above code could generate a single comparison, even in debug mode. (And assigning a value becomes a more complicated transformation than writing Contrast this with nullable pointers (and nullable function pointers), where they are guaranteed to be represented as a |
The
==
and!=
operators should work when the LHS and RHS are any combination of&T
and?&T
.The text was updated successfully, but these errors were encountered: