-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Open
Description
Variable declarations under disjunctive patterns
- Specification: https://github.com/dotnet/csharplang/blob/main/proposals/pattern-variables.md
- Discussion: Proposal: Permit variable declarations under disjunctive patterns #8621
Summary
-
Allow variable declarations under
or
andnot
patterns and acrosscase
labels in aswitch
section to share code.if (e is C c or Wrapper { Prop: C c }) return c; Expr Simplify(Expr e) { switch (e) { case Mult(Const(1), var x): case Mult(var x, Const(1)): case Add(Const(0), var x): case Add(var x, Const(0)): return Simplify(x); // .. } }
Instead of:
if (e is C c1) return c1; if (e is Wrapper { Prop: C c2 }) return c2; Expr Simplify(Expr e) { switch (e) { case Mult(Const(1), var x): return Simplify(x); case Mult(var x, Const(1)): return Simplify(x); case Add(Const(0), var x): return Simplify(x); case Add(var x, Const(0)): return Simplify(x); // .. } }
-
Also relax single-declaration rules within expression boundaries as long as each variable is assigned once.
if (e is C c || e is Wrapper { Prop: C c }) ; if (b ? e is C c : e is Wrapper { Prop: C c }) ;
-
Also relax single-declaration rules within conditions of an
if/else
:if (e is C c) { } else if (e is Wrapper { Prop: C c }) { }