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
Allow ref assignment for switch expressions #3326
Comments
@CyrusNajmabadi for the bad VS suggestion. |
@alrz :) |
@333fred I believe the bad VS suggestion has already been addressed by dotnet/roslyn#40236. @CyrusNajmabadi recommended I open a new issue here for the actual language feature request. |
It seems inconsistent that you can do this with the ? operator but not with switch expressions I am able to do the following with the ? operator
It just feels wrong that you are not able to do this in switch expressions |
To be clear, there would be few more
This is of course for consistency with standard/existing ref-local syntax applied to your ternary example:
So now the only question is, when might it happen? I was actually quite surprised to discover this lurking omission, having been spoiled, in general, by the always impossibly-stellar C# design enterprise. |
This issue is the any time bucket, meaning that it is open for community contributions (and likely won't happen until then). Currently, it needs an approved specification. |
Adding to this, should the ability to specify return direction switch ref
{
Direction.North => NorthField,
Direction.South => SouthField,
Direction.East => EastField,
Direction.West => WestField,
_ => throw new NotImplementedException(),
}; Obviously the |
I've started creating a primitive spec for the current standing of the proposal to help this move forward. |
FYI this is extremely unlikely to fly. The rules should be exactly the same as ref ternaries. |
Now that I'm thinking about it, it makes sense to require that. I'll have it changed. EDIT: Done |
I'll champion this. |
@Rekkonnect WRT to your spec, how close is it to the language/approach taken for ref-conditional expressions? Can you make a PR on this, and also link to the relevant existing ref-specs so we can compare? |
This idea was already brought up in #2507, but the conclusion there was that:
However, switch expressions definitely still do not work the same way as ternary operators with respect to ref assignment, as others have noted in the Roslyn repo:
Long story short, suppose I have a
Direction
enum and want to conditionally do a ref assignment by switching on aDirection
instance, like so:Currently, Visual Studio suggests converting the switch statement to an expression, generating the following invalid code:
Each ref expression has a red squiggly saying
Invalid expression term 'ref'
, even if I addref
after thereturn
. If I keep theref
after thereturn
and delete all the case-specificref
s, then the whole switch block gets squiggled with the messageAn expression cannot be used in this context because it may not be passed or returned by reference
.I won't say "this should be easy to implement", cause I'm sure its not, but I see no reason why a ref assignment/return with a switch expression should be invalid syntax. Based on the conversation in #2507, I'm guessing the most likely implementation is to require ref after the return and in each case expression, but less typing is always better 🙂
The text was updated successfully, but these errors were encountered: