Version 1 (modified by manualwiki, 13 years ago) (diff) |
---|
Inline function
The inline function refactoring step substitutes the selected application with the corresponding function body and executes compensations. The function may consist of one or more function clauses and may have guard expression(s), inline can handle these cases. In the example the sort/1 application is inlined. The application is replaced with a case expression.
-module(in). -export([sort/2]). sort(A, B) -> sort({A, B}). sort({A, B}) when A > B -> {A, B}; sort({A, B}) -> {B, A}.
sort/1 inlined:
-module(in). -export([sort/2]). sort(A, B) -> case {A, B} of {A, B} when A > B -> {A, B}; {A, B} -> {B, A} end. sort({A, B}) when A > B -> {A, B}; sort({A, B}) -> {B, A}.
Side conditions
- Applying the inline function must not cause variable name conflicts. A variable name conflict arises when the same variable name is used in the body of the function clause and in the scope of the selected function application, except the variables which are bound in the formal parameters where the structure of the formal and the actual parameters are equivalent.
- If the function is defined in other module:
- the function do not contain local (not exported) function applications in its body.
- macro name conflicts must not occur in the current module, that is, macro names used in the functions must refer to the same macro definition in the current and in the definition module. This applies to macros used in these macros too.
- record name conflicts must not occur in the current module, that is, record names used in the functions must refer to the same record definition in the current and in the definition module.
- If the user does not specify a function application or the specified function does not exist, the transformation starts an interaction to ask the user to specify one. The user has to select a function from a radio group.
Transformation steps and compensations
- Find the corresponding function definition and copy the function clause(s) and create (if it is needed) a corresponding structure from the expressions of the body(ies), the guard expressions (if there is any) and from the patterns of the function clause(s).
- Where the actual and formal parameters are structurally equivalent, create variable name pairs and rename the corresponding variables in the copied body.
- Where the formal and structural parameters are not equivalent, create a match expression from these parameters. The left hand side is a tuple from the formal parameters, and the right hand side is a tuple from the actual parameters.
- If the function consists of
- one clause and does not have guard expression and the body of the function contains only one expression and match expressions should not be created from the parameters, replace the application with this single expression.
- one clause and does not have guard expression and the body of the function contains more than one expression and the parent expression of the selected application is a clause, replace the application with the sequence of the expressions from the body of the function clause extended with the created match expression.
- one clause and does not have guard expression and the body of the function contains more than one expression and the selected application is a subexpression:
- create a begin-end block from the sequence of the expressions from the body of the function clause extended with the created match expression
- replace the application with this begin-end block.
- more than one clause, or it has guard expressions (or both) or variables appear multiple times with the same name in a pattern list:
- create a case expression from the function clause body(ies) expression(s), guard and pattern expressions.
- replace the application with this case expression.
- If the definition of the function is in another module
- qualify the applications in the copied body which call exported functions from the defining module.
- qualify the applications in the copied body which call imported functions.
- copy or import record and macro definitions to the current module which are used in the copied body(ies).