Skip to content

[Proposal]: Variable declarations under disjunctive patterns #8622

@333fred

Description

@333fred

Variable declarations under disjunctive patterns

Summary

  • Allow variable declarations under or and not patterns and across case labels in a switch 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 }) { }

Design meetings

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions