| 1 | = Expand fun expression = |
| 2 | |
| 3 | The “Expand fun expression” transformation expands an implicit fun expression into an explicit one. This transformation can be done separately, but sometimes it is needed by other transformations as a compensation step. One such example is the module-qualifier correction in the “move function” refactoring. When an implicit fun expression need module qualifying, the result will be like this: {{{fun module:function/2}}}. This form is not supported in older Erlang versions. But when we expand the fun expression, a simple function application appear instead of the short form ({{{fun function/2}}} equals to {{{fun(V1, V2) -> function(V1, V2) end}}}, and it will become able to module qualifying. |
| 4 | |
| 5 | When we want to modify the function application or just don’t want to use this syntactical sugar (the implicit form of fun expression), we can use this transformation to expand the expression. |
| 6 | |
| 7 | Expanding the fun expression {{{fun far:away/2}}}: |
| 8 | |
| 9 | {{{ |
| 10 | -module(near). |
| 11 | |
| 12 | -export([f/0]). |
| 13 | |
| 14 | f() -> |
| 15 | fun far:away/2. |
| 16 | }}} |
| 17 | |
| 18 | Result: |
| 19 | |
| 20 | {{{ |
| 21 | -module(near). |
| 22 | |
| 23 | -export([f/0]). |
| 24 | |
| 25 | f() -> |
| 26 | fun(V1, V2) -> |
| 27 | far:away(V1, V2) |
| 28 | end. |
| 29 | }}} |
| 30 | |
| 31 | == Side conditions == |
| 32 | |
| 33 | * The selected expression should be an implicit fun expression or part/subexpression of an implicit fun expression. |
| 34 | |
| 35 | |
| 36 | == Transformation steps and compensations == |
| 37 | |
| 38 | 1. If the implicit fun expression is found, the new syntax structure is created and the old expression is replaced with the new one. |