|
- Overview
- How to do it?
- Adding syntaxes to a procedure
- Deleting a procedure syntax
- Managing the overload at run time
- Basic mechanism: dynamic determination of the syntax according to the number and type of parameters
- Dynamic dispatch
- Virtual methods
- Notes
- Notes
- Scope of procedures
Prototype overload/Overload
Adding syntaxes to a procedure To add a syntax to an existing procedure: - In the project explorer, select the procedure.
- Display the popup menu of the procedure and select "Add a syntax".
- A new syntax is automatically created in the code editor.
Remarks: - The creation of a procedure with the same name automatically proposes to add a new syntax to the existing procedure.
- If a procedure has several syntaxes, the number of syntaxes is displayed in the project explorer (beside the name of the procedure).
Deleting a procedure syntax To delete a syntax: - Display the code of the procedure in the code editor.
- In the bar of the syntax, select "Delete" from the popup menu.
- You can:
- delete the current syntax.
- delete all the syntaxes (in this case, the procedure is deleted).
Managing the overload at run time Basic mechanism: dynamic determination of the syntax according to the number and type of parameters The runtime engine searches for the syntax: - that has the same number of parameters.
- that has the minimum number of conversions.
If two syntaxes are equivalent, the first one in the order of the code editor is run. Basic example
// First syntax PROCEDURE p(s is string)
// Second syntax PROCEDURE p(n is int)
// Third syntax PROCEDURE p(n is int, s is string)
// Fourth syntax PROCEDURE p(s is string, n is int)
// Calls
// Calls the first syntax (best corresponding syntax) p("A")
// Calls the second syntax (best corresponding syntax) p(1)
// Calls the third syntax (best corresponding syntax) p(1,"A")
// Calls the fourth syntax (best corresponding syntax) p("A",1)
// Calls the third syntax // (equivalent to the fourth one, the third one has priority because described before) p(1,1)
// Calls the third syntax // (equivalent to the fourth one, the third one has priority because described before) p("A","A")
Dynamic dispatch For a procedure with several syntaxes whose parameters expect class instances, the runtime engine is using the "Dynamic dispatch" method to define the syntax that must be called. Let's study the following example: - a "BaseClass" class
- two classes, "DerivedClass1" and "DerivedClass2", that inherit from "BaseClass".
// First syntax PROCEDURE p(LOCAL p is BaseClass)
// Second syntax PROCEDURE p(LOCAL p is DerivedClass1)
// Calls pBase is dynamic BaseClass pBase = new BaseClass // Initialization p(pBase) // First syntax pBase = new DerivedClass1 // Initialization p(pBase) // Second syntax pBase = new DerivedClass2 // Initialization p(pBase) // First syntax
Virtual methods To manage virtual methods, several aspects can be taken into account: - 1st aspect: a syntax of the method of the derived class redefines a syntax of the method of the base class
BaseClass PROCEDURE meth(s is string) PROCEDURE meth(n is int)
DerivedClass PROCEDURE meth(n is int)
// Call oBase is BaseClass // Calls the first syntax in the BaseClass class oBase.meth("A") // Calls the second syntax in the BaseClass class oBase.meth(1)
oDerived is DerivedClass // Calls the first syntax in the BaseClass class oDerived.meth("A") // Calls the first syntax in the DerivedClass class oDerived.met (1)
- 2nd aspect: an additional syntax in the method of the derived class
BaseClass PROCEDURE meth(p) PROCEDURE meth(s is string)
DerivedClass PROCEDURE meth(n is int)
// Call oBase is BaseClass // Calls the second syntax in the BaseClass class oBase.meth("A") // Calls the first syntax in the BaseClass class oBase.meth(1)
oDerived is DerivedClass // Calls the second syntax in the BaseClass class oDerived.meth("A") // Calls the first syntax in the DerivedClass class oDerived.met (1)
- 3rd aspect: special case when the method of the base class and the method of the derived class have a single syntax with different prototypes:
BaseClass PROCEDURE meth(s is string)
DerivedClass PROCEDURE meth(n is int)
The compiler cannot decide whether the method of the derived class is an override of the method of the base class or a new syntax.- To trigger an override, the attribute with the <override> extension must be added to the method of the derived class.
PROCEDURE method(...) <override>
- To trigger an overload, the attribute with the <overload> extension must be added to the method of the derived class.
PROCEDURE method(...) <overload>
Notes - In most cases, you must force the parameters to LOCAL to respect the WLanguage rules for passing parameters.
- If the dynamic determination of the syntax finds a compatible syntax, this one can trigger a runtime error on the rules for passing parameters by reference.
Scope of procedures The global procedures and the class method can be public, protected or private. For an overloaded procedure, the scope must be the same for all the syntaxes. A compilation error occurs if the scopes of the syntaxes are different.
This page is also available for…
|
|
|
| |
| Click [Add] to post a comment |
|
| |
|
| |
| |
| |
| |
| |
| |
| | |
| |