Skip to content

missing update operators (<op>=) #20422

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

Closed
Radvendii opened this issue Jun 26, 2024 · 8 comments
Closed

missing update operators (<op>=) #20422

Radvendii opened this issue Jun 26, 2024 · 8 comments
Labels
proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.

Comments

@Radvendii
Copy link

Radvendii commented Jun 26, 2024

Zig Version

0.14.0-dev.93+ccd3cc326

Steps to Reproduce and Observed Behavior

||=, and=, or=, ++=, **= do not exist, but as far as I can tell this is just an oversight.

Expected Behavior

Any operator with a result type the same as it's first argument would have an <op>= variant with x <op>= y is the same as x = x <op> y.

@Radvendii Radvendii added the bug Observed behavior contradicts documented or intended behavior label Jun 26, 2024
@Radvendii
Copy link
Author

I've realised ++=, and **= don't make sense because the return type is not the same as the first argument. The statement still holds for ||=, and= and or= though.

@mlugg
Copy link
Member

mlugg commented Jun 26, 2024

[...] but as far as I can tell this is just an oversight.

What makes you think that? See also #5804 and #16511.

||= would be a weird operator because it's very rare to have a var x: type, let alone be combining error sets using a mutable variable like this. It could exist, but its use would be incredibly limited, and it would probably lead to further confusion between || and or.

++= and **= you've noted the issue with, and are already tracked by the issue linked above.

and= and or= would be quite weird operators because of the short-circuiting behavior of and and or. Zig has a rule that keyword operators are precisely the operators which perform control flow; where, then, do and= and or= lie, containing both keywords and symbols? It seems like they must surely have short-circuiting behavior (i.e. x and= expr does not evaluate expr if x is false), but this is also kind of unintuitive. Also, these operators just look weird -- Zig today doesn't have any operator which contains a mix of symbols and word characters.

Do you have a use case where these operators are particularly useful? Bear in mind that Zig is a language focused heavily on readability -- code being faster to write isn't typically a good justification for a new langauge feature, particularly if readability is harmed (and I would argue these operators absolutely harm readability).

@mlugg mlugg added proposal This issue suggests modifications. If it also has the "accepted" label then it is planned. and removed bug Observed behavior contradicts documented or intended behavior labels Jun 26, 2024
@edqx
Copy link

edqx commented Jun 26, 2024

Agree with mlugg about the short circuiting behaviour being unintuitive.

I think another weird part about and and or is that where it's useful in a language like JS to assign a default value for ||= (or=) or "update where exists" in the case of &&= (and=), this just isn't a thing in Zig since there's no truthiness, so I'd like to see what use case there really is for this...

Only thing I can think of is to somehow "build" boolean expressions, but I don't think I'd want to encourage that anyway..

@Radvendii
Copy link
Author

Honestly, I came to this issue just from ++=. I was building a tuple at comptime and thought it would work.

Only then did I realise there were other operators missing and thought it might be a more general thing. But it seems like there are different reasons for different ones not being included. Thanks.

@nektro
Copy link
Contributor

nektro commented Jun 26, 2024

I've realised ++=, and **= don't make sense because the return type is not the same as the first argument

this isn't necessarily true; but also its rare enough in use that it's prolly fine to not add new syntax

@edqx
Copy link

edqx commented Jun 26, 2024

I'll say that I've actually had quite a lot use personally for "++="-ing. For one, it makes it very easy to construct fields for reifying struct types.

@Radvendii
Copy link
Author

Can y'all give an example of where it would be valid to use? Where today you use foo = foo ++ bar.

@mlugg
Copy link
Member

mlugg commented Jun 26, 2024

Please read #16511 and direct further discussion of ++= / **= there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Projects
None yet
Development

No branches or pull requests

4 participants