Pattern matching order of evaluation #5487
-
The proposal document for recursive pattern matching has a section titled "Order of evaluation in pattern matching" which implies that the compiler is free to optimize and re-order the evaluation of properties. However it doesn't seem to make the guarantee for anything other than
The section on positional matching has a sentence at the very end that states:
As this is only a proposal specification, I'm wondering if anything has changed about it or if it's the official stance. The official Patterns documentation only refers to order of evaluation when discussing Logical Patterns using nested
Reordering properties in a match where one of the properties throws, seems to work and follow left-to-right evaluation. That may be an implementation detail or perhaps there's nothing to optimize in my pattern. Either way, I'm hoping to get clarification so I don't make any assumptions going forward BackgroundI actually hit a case when matching against a property that (unknown to me at the time) threw an exception. I was trying to pull the conn is { Version: { } v } I tried changing it to: conn is { Version: {} v, State: ConnectionState.Open } This didn't work, however, and threw an exception if the connection wasn't open. So I changed it to the following, which does work: conn is { State: ConnectionState.Open, Version: {} v } This got me wondering about what guarantees exist and whether or not I could rely on the above behavior. I think the documents I linked answers the question for me, but because one is "proposal" and neither seem to make a blanket statement on the topic, I just wanted to be sure. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 6 replies
-
No such guarantees exist. As the specification says, the ordering is unspecified, and we are free to change it in the future. I do think we would have a very hard time changing it (because code like this will be written and will break on update) but we have done breaking changes before with big enough motivation, so if we find a really good motivation, it is possible pattern evaluation would change. |
Beta Was this translation helpful? Give feedback.
No such guarantees exist. As the specification says, the ordering is unspecified, and we are free to change it in the future. I do think we would have a very hard time changing it (because code like this will be written and will break on update) but we have done breaking changes before with big enough motivation, so if we find a really good motivation, it is possible pattern evaluation would change.