Skip to content

Latest commit

 

History

History
69 lines (49 loc) · 1.88 KB

File metadata and controls

69 lines (49 loc) · 1.88 KB

ref assignment on switch expressions

  • Proposed
  • Prototype: None
  • Implementation: None
  • Specification: Started, below

Summary

Provide the ability to return ref values in switch expressions.

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

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.

Examples

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,
    };
}

Drawbacks

None.

Alternatives

Currently, this may only be achieved with a switch statement, or a sequence of if-else statements.

Unresolved questions

  • Requires LDM review
  • Should there be a quicker way to denote returning a ref to the provided expression by not requiring copying it over for each case?

Design meetings

None.