|
|
|
|
|
- Overview
- How to?
- Adding syntaxes to a procedure
- Deleting a procedure syntax
- Managing the overload at runtime
- 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
The procedures and the methods of classes can have several syntaxes. For example, a procedure can have: - a syntax that takes a string in parameter.
- a syntax that takes an integer in parameter.
Therefore, several syntaxes can exist for the same procedure or for the same method with different parameters and code. At run time, the engine automatically defines the syntax to call according to the number and to the type of the parameters passed. This technology is presented under several names and it includes different purposes. The following terms can be used: - Overload,
- Prototype overload,
- Overload,
- Dynamic dispatch,
- Parametric polymorphism,
- etc.
This feature is available for: - The global procedures.
- The local procedures.
- The class methods including the Constructors.
![Android Android](https://doc.windev.com/ext/images/us/ANDROID.png?385929803) The constructors of derived classes cannot be overloaded.
In the rest of this document: - we will use the term overload.
- the "Procedure" keyword will be used to identify a global procedure, a local procedure or a method.
Adding syntaxes to a procedure To add a syntax to an existing procedure: - In the project explorer, select the procedure.
- Open the context 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 runtime 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 PROCÉDURE p(s is string)
// Second syntax PROCÉDURE p(n is int)
// Third syntax PROCÉDURE p(n is int, s is string)
// Fourth syntax PROCÉDURE 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 PROCÉDURE p(LOCAL p is BaseClass)
// Second syntax PROCÉDURE 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 PROCÉDURE meth(s is string) PROCÉDURE meth(n is int)  DerivedClass PROCÉDURE 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 PROCÉDURE meth(p) PROCÉDURE meth(s is string)  DerivedClass PROCÉDURE 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 PROCÉDURE meth(s is string)  DerivedClass PROCÉDURE 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…
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|