Skip to content
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

[Proposal]: Variable declarations under disjunctive patterns #8622

Open
333fred opened this issue Nov 19, 2024 · 0 comments
Open

[Proposal]: Variable declarations under disjunctive patterns #8622

333fred opened this issue Nov 19, 2024 · 0 comments
Assignees
Milestone

Comments

@333fred
Copy link
Member

333fred commented Nov 19, 2024

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

@333fred 333fred added this to the Working Set milestone Nov 19, 2024
@333fred 333fred self-assigned this Nov 19, 2024
@dotnet dotnet locked and limited conversation to collaborators Nov 19, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

1 participant