|
|
|
|
|
- Overview
- How to?
- Adding syntaxes to a procedure
- Deleting a procedure syntax
- Managing the overload at runtime
- Basic mechanism: dynamic determination of syntax depending on 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 as parameter.
- a syntax that takes an integer as parameter.
Therefore, several syntaxes can exist for the same procedure or for the same method with different parameters and code. At runtime, 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.
New in version 2025 Derived class constructors can now be multi-syntax.
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" in the context 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 syntax depending on the number and type of parameters The runtime engine finds 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
PROCEDURE p(n is int, s is string)
PROCEDURE p(s is string, n is int)
p("A")
p(1)
p(1,"A")
p("A",1)
p(1,1)
p("A","A")
Dynamic dispatch For a procedure with several syntaxes whose parameters expect class instances, the runtime engine uses the "Dynamic dispatch" method to define the syntax that must be called. Consider the following example: - a "BaseClass" class
- two classes, "DerivedClass1" and "DerivedClass2", that inherit from "BaseClass".
PROCEDURE p(LOCAL p is ClasseBase)
PROCEDURE p(LOCAL p is ClasseDérivée1)
pBase is ClasseBase dynamic
pBase = new ClasseBase
p(pBase)
pBase = new ClasseDérivée1
p(pBase)
pBase = new ClasseDérivée2
p(pBase)
Virtual methods To manage the virtual methods, several aspects can be taken into account: - 1st aspect: a derived class method syntax redefines a base class method syntax
ClasseBase
PROCEDURE meth(s is string)
PROCEDURE meth(n is int)
ClasseDérivée
PROCEDURE meth(n is int)
oBase is ClasseBase
oBase.meth("A")
oBase.meth(1)
oDérivée is ClasseDérivée
oDérivée.meth("A")
oDérivée.meth(1)
- 2nd aspect: additional syntax in the derived class method
ClasseBase
PROCEDURE meth(p)
PROCEDURE meth(s is string)
ClasseDérivée
PROCEDURE meth(n is int)
oBase is ClasseBase
oBase.meth("A")
oBase.meth(1)
oDérivée is ClasseDérivée
oDérivée.meth("A")
oDérivée.meth(1)
- 3rd aspect: special case when the base class method and the derived class method each have a single syntax with different prototypes:
ClasseBase
PROCEDURE meth(s is string)
ClasseDérivée
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 méthode(...) <redéfinition> - To trigger an overload, the attribute with the <overload> extension must be added to the method of the derived class.
PROCEDURE méthode(...) <multisyntaxe>
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…
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|