- Proposed
- Prototype: None
- Implementation: None
- Specification: Started, below
Provide the ability to return ref values in switch expressions.
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.
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. The examples below cover this case too. This design aligns with the current design in the ternary operator.
throw expressions can still be used normally.
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(),
};
private void RefSwitchAssignLocal()
{
ref int dimension = ref axis switch
{
Axis.X => ref X,
Axis.Y => ref Y,
};
}None.
Currently, this may only be achieved with a switch statement, or a sequence of if-else statements.
- Requires LDM review
- Should there be a quicker way to denote returning a
refto the provided expression by not requiring copying it over for each case?
None.