PC SOFT

WINDEVWEBDEV AND WINDEV MOBILE
ONLINE HELP

Home | Sign in | English UK
  • Overview
  • How to proceed?
  • 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
WINDEV
WindowsLinuxUniversal Windows 10 AppJavaReports and QueriesUser code (UMC)
WEBDEV
WindowsLinuxPHPWEBDEV - Browser code
WINDEV Mobile
AndroidAndroid Widget iPhone/iPadApple WatchUniversal Windows 10 AppWindows Mobile
Others
Stored procedures
Overview
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.
In the rest of this document:
  • the term of multi-syntax will be used.
  • the "Procedure" keyword will be used to identify a global procedure, a local procedure or a method.
Versions 17 and later
iPhone/iPad This feature is available for the iPhone/iPad applications.
New in version 17
iPhone/iPad This feature is available for the iPhone/iPad applications.
iPhone/iPad This feature is available for the iPhone/iPad applications.
Versions 18 and later
Universal Windows 10 App This feature is available in Windows Store apps mode.
New in version 18
Universal Windows 10 App This feature is available in Windows Store apps mode.
Universal Windows 10 App This feature is available in Windows Store apps mode.
Versions 21 and later
Universal Windows 10 App This feature is available in Universal Windows 10 App mode.
Apple Watch This feature is available in Apple Watch mode.
New in version 21
Universal Windows 10 App This feature is available in Universal Windows 10 App mode.
Apple Watch This feature is available in Apple Watch mode.
Universal Windows 10 App This feature is available in Universal Windows 10 App mode.
Apple Watch This feature is available in Apple Watch mode.
How to proceed?

Adding syntaxes to a procedure

To add a syntax to an existing procedure:
  1. In the project explorer, select the procedure.
  2. Display the popup menu of the procedure and select "Add a syntax".
  3. A new syntax is automatically created in the code editor.
Notes:
  • 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:
  1. Display the code of the procedure in the code editor.
  2. In the bar of the syntax, select "Delete" from the popup menu.
  3. 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.
Notes

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.
Minimum version required
  • Version 16
This page is also available for…
Comments
Click [Add] to post a comment