| 1 | = Merge expressions = |
| 2 | |
| 3 | During the merge expressions transformation, a new match expression is created that binds the selected expression to the variable that the user has given as input, and all instances of the expression is changed to the variable. |
| 4 | |
| 5 | Example of Merge Expressions transformation: |
| 6 | |
| 7 | {{{ |
| 8 | foo(A,B) -> |
| 9 | |
| 10 | peer ! {note, A+B}, |
| 11 | A+B. |
| 12 | }}} |
| 13 | |
| 14 | Result: |
| 15 | |
| 16 | {{{ |
| 17 | foo(A,B) -> |
| 18 | V = A+B, |
| 19 | peer ! {note, V}, |
| 20 | V. |
| 21 | }}} |
| 22 | |
| 23 | Merge expression duplicates refactoring is executed in the function, {{{A+B}}} expression in the Message-Passing {{{(peer ! note,A,B)}}} is stored in {{{V}}} variable, then all instances of the expression are changed to {{{V}}} variable. |
| 24 | |
| 25 | == Side conditions == |
| 26 | |
| 27 | * The expression cannot be substituted if any of its sub-expressions have side effects. |
| 28 | |
| 29 | * The transformation cannot be executed if the expression is in the head of a list comprehension, in a pattern or in a guard expression. |
| 30 | |
| 31 | * If the expression occurs in a generator expression, it should not contain variables that are bound by generator patterns. |
| 32 | |
| 33 | * The given variable name should not already exist in the given scope in order to avoid name clashes. |
| 34 | |
| 35 | * If the given variable name is not legal then the transformation starts an interaction to ask for a new variable name. |
| 36 | |
| 37 | |
| 38 | == Transformation steps and compensations == |
| 39 | |
| 40 | 1. Determine the insertion point. The insertion point is the first possible location within a body where all of the variables of the expression have already received their binding. If the selected expression contains no variables, the insertion point may be in any containing scope; currently the innermost scope is chosen. If the selected expression contains at least one variable, the insertion point is in the outermost scope that contains all variables. |
| 41 | |
| 42 | 2. Insert a new match expression to the insertion point that binds the expression to the new variable. If there is an expression to be replaced at this position, the match expression should replace it. |
| 43 | |
| 44 | 3. Substitute all other instances of the expression to the new variable. Note that not all expressions whose structure is identical to the expression are instances of the expression: all variables have to have the same binding, and therefore the same scope. These substitutions should remove surrounding parentheses from instances. |