-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Ref assignment switch expressions #7352
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
Open
Rekkonnect
wants to merge
9
commits into
dotnet:main
Choose a base branch
from
Rekkonnect:ref-assignment-switch-expressions
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+235
−0
Open
Changes from 3 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
7d2dc7d
Create spec for ref assignment on switch expressions
Rekkonnect a0eeaa1
Do include a ref in front of the expression
Rekkonnect 011f974
Include more details in the spec
Rekkonnect 827884d
Update rules about `ref readonly` + examples
Rekkonnect 1d66a5c
Update grammar adjustment information + fix words
Rekkonnect 24caf81
Promote ref discard to error
Rekkonnect 0783829
More rules
Rekkonnect f2bfc62
Clarify default switch arm
Rekkonnect ef422c4
Improve ref safety description
Rekkonnect File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| # `ref` assignment on `switch` expressions | ||
|
|
||
| * [x] Proposed | ||
| * [ ] Prototype: None | ||
| * [ ] Implementation: None | ||
| * [ ] Specification: Started, below | ||
|
|
||
| ## Summary | ||
| [summary]: #summary | ||
|
|
||
| Provide the ability to return `ref` values in `switch` expressions. | ||
|
|
||
| ## Motivation | ||
| [motivation]: #motivation | ||
|
|
||
| The `switch` expression is meant to provide a quicker way to return values based on a given expression, but does not support returning `ref` expressions. This limitation doesn't prevent any harm, and can be lifted. | ||
|
|
||
| ## Detailed design | ||
| [design]: #detailed-design | ||
|
|
||
| A `switch` expression may return a `ref` value. The returning expression, if not a `throw` expression, must be a `ref` expression. | ||
|
|
||
| A `switch` expression that returns a `ref` value needs an extra `ref` in front of it when assigned/returned to a `ref` local. This design aligns with the current design in the ternary operator. | ||
|
|
||
| `throw` expressions can still be used normally, and pose no effect on the return type of the `switch` expression. | ||
|
|
||
| Once a single switch arm returns a `ref` expression, all other switch arms must return `ref` expressions, unless they are `throw` expressions. Not providing a `ref` expression in a switch arm causes a compiler error. | ||
|
|
||
| Since all ref expressions are LValues, and the switch arms are all ref expressions, the entire result of the switch expression is also an LValue. Therefore, the entire switch expression can be passed by reference, assigned to, and returned by reference. Note that for directly assigning to the switch expression's ref result, the `ref` keyword must not be used, like normal ref locals are assigned. For example: | ||
|
|
||
| `item switch { 1 => ref X, 2 => ref Y } = value` | ||
Rekkonnect marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| When not in the context of assigning the ref result of the `switch` expression, not using the `ref` keyword in front of the switch expression is considered a logical error. Thus, it is advised to show a warning covering that case, like in the example: | ||
| ```csharp | ||
| private void RefSwitchAssignLocal() | ||
| { | ||
| // Warning: | ||
| // the switch statement returns by reference but the result is immediately dereferenced | ||
| int dimension = axis switch | ||
| { | ||
| Axis.X => ref X, | ||
| Axis.Y => ref Y, | ||
| }; | ||
| } | ||
| ``` | ||
|
|
||
| Ref return safety relies on the individual switch arms. If any of the switch arms that does not throw is unsafe to return, the entire switch statement is unsafe to return. Otherwise, the entire `switch` statement's expression is safe to return. | ||
|
|
||
| ## Examples | ||
| [examples]: #examples | ||
|
|
||
| ```csharp | ||
| // Returning by reference | ||
| private ref int GetDirectionField(Direction direction) => ref direction switch | ||
| { | ||
| Direction.North => ref NorthField, | ||
| Direction.South => ref SouthField, | ||
| Direction.East => ref EastField, | ||
| Direction.West => ref WestField, | ||
| _ => throw new NotImplementedException(), | ||
| }; | ||
|
|
||
| // Assigning to a ref local | ||
| private void RefSwitchAssignLocal() | ||
| { | ||
| ref int dimension = ref axis switch | ||
| { | ||
| Axis.X => ref X, | ||
| Axis.Y => ref Y, | ||
| }; | ||
| } | ||
Rekkonnect marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // Assigning to the ref of the switch expression | ||
| private void RefSwitchAssignDirectly(int value) | ||
| { | ||
| axis switch | ||
| { | ||
| Axis.X => ref X, | ||
| Axis.Y => ref Y, | ||
| } = value; | ||
|
|
||
| // Equivalent to: | ||
| ref int dimension = ref axis switch | ||
| { | ||
| Axis.X => ref X, | ||
| Axis.Y => ref Y, | ||
| }; | ||
| dimension = value; | ||
| } | ||
| ``` | ||
|
|
||
| ## Drawbacks | ||
| [drawbacks]: #drawbacks | ||
|
|
||
| None. | ||
|
|
||
| ## Alternatives | ||
| [alternatives]: #alternatives | ||
|
|
||
| Currently, this may only be achieved with a `switch` statement, or a sequence of `if`-`else` statements. | ||
|
|
||
| ## Unresolved questions | ||
| [unresolved]: #unresolved-questions | ||
|
|
||
| - [ ] Requires LDM review | ||
|
|
||
| ## Design meetings | ||
| [meetings]: #design-meetings | ||
|
|
||
| None. | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.