-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
make assignment an expression that returns the payload lvalue #1022
Comments
How does this interact with the "expression value ignored" error? surely you wouldn't have to: _ = (maybe = foo); |
I hadn't thought of that. I suppose that there would be an exception for this expression. I'm open minded to a syntax counter proposal as well. |
I this struct case: nullable_foo = Foo(undefined);
const ptr = &??nullable;
initializePoint(&nullable.point); could we say initializePoint(&nullable??point);
nullable??point.x = 9; // a unreachable
nullable?point.x = 9; // a nop? this does scare me a little though That would unwrap the null or be unreachable. As for named return types could something like this.ReturnType, fn.ReturnType, or @typeof(this).ReturnType be used as a short cut |
Also is this going to open up using assignment in conditional expressions or passed as arguments? |
We could leave it out of the assignment statement and hide it behind a keyword:
Note: |
I think copy elision (#287) mostly solves this, and we can get by without it. |
#This is not for syntax convenience, this is for solving a very particular problem with nullables,
union(enum)
, and error unions.When we want to read from a nullable we have lots of good options:
Similarly for
union(enum)
:However we run into a problem when writing to these values:
How do we use
initializePoint
?This is the best thing we have:
There are several problems with this:
Once we have written a non null value, we should be able to write
to the payload.
Same thing for
union(enum)
. Once we have chosen an active field,the payload should be writable.
Here's my proposal to fix this:
union(enum)
, and error unionsThat's it. Then you can do this:
Here's how it would look for a
union(enum)
:The workaround for
union(error)
is not bad since we can address fields:If we had named return values (See #286), which would be useful for copy elision (See #287),
then we would want to use this feature for the result. For example:
The workaround for error unions is particularly ugly:
The text was updated successfully, but these errors were encountered: