-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Allow equality operator (== and !=) for ?T and T when equality operator is supported for T and T #1332
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
It doesn't work, because of issue ziglang#1332.
What should happen if one side is Should Edit: I suppose the |
A similar question is whether |
It's a bug if not. |
For example, when comparing two distinct optional values: const x: ?i32 = createAnOptional();
const y: ?i32 = createAnOptional();
if (x == y) {
debug.warn("equal", .{});
} It would be necessary to turn the ((x == null) == (y == null)) and (x == null or x.? == y.?) But if one were to write this instead: if (x == 10) { ... } What should that expand to? Should the (lhs != null) and (lhs.? == rhs) Which is likely a lot more efficient. |
@theInkSquid this proposal is for comparing This proposal is to expand What you're proposing for |
I see, thank you. Would it be appropriate to short-circuit evaluate the non-optional parameter? For example: const x: ?i32 = null;
if (x == @compileError("err")) {...} Should this be a compile error? Or, since it can be determined that because I am guessing this should not short-circuit, since that would be a non-keyword operator affecting control flow. Also, it seems a bit useless and confusing, but I just wanted to check. |
@theInkSquid that's an insightful question. There are two options. For the expression Option 1: no short circuit.
Option 2: short circuit:
The OP posted an important motivation for this issue: the inconsistency of the EDIT: An even stronger argument for no short circuiting is that the |
to clarify, |
Yes |
How about when Like could I do this? var x: ?i32 = ...;
var y: i16 = ...;
x == y |
Also, what about for other special cases where a type is comparable with some different other type? For example, you can compare a tagged union with an enum literal to check if that is the current value. Should an optional tagged union be comparable with an enum literal? That doesn't fall into the rigid If something like If we allow Even more powerful, if we allow ?i32 == ?i32
T = i32, U = ?i32 now we check if |
This test passes just fine:
While this one fails to compile, with the error message
"error: operator not allowed for type '?i32'"
:At first, I assumed this was a bug, but reading #658, it sounds like this was a deliberate decision?
(Because ?i32 == i32 would have to do two comparisons behind the scenes, while ?*i32 == *i32 can just do one.)
Still, this seems like a surprising inconsistency -- in every other language I can think of with a generic option type, either (1)
Option<T>
supports==
iffT
supports==
, or (2)Option<T>
doesn't support==
at all. Here, we have?T
supporting==
for one primitive type (pointers) but not for an even more primitive type (i32
).The text was updated successfully, but these errors were encountered: