Overloading, overriding, and extending functions and events

In PowerBuilder, when functions are inherited, you can choose to overload or override the function definition, described in Overloading and overriding functions.

When events are inherited, the scripts for those events are extended by default. You can choose to extend or override the script, described in Extending and overriding events.

Overloading and overriding functions

To create an overloaded function, you declare the function as you would any function using Insert>Function.

Overriding means defining a function in a descendant object that has the same name and argument list as a function in the ancestor object. In the descendant object, the function in the descendant is always called instead of the one in the ancestor -- unless you use the scope resolution operator (::).

To override a function, open the descendant object in the painter, select the function in the Script view, and code the new script. The icon that indicates that there is a script for a function is half shaded when the function is inherited from an ancestor.

You can overload or override object functions only -- you cannot overload global functions.

Type promotion when matching arguments for overloaded functions

When you have overloaded a function so that one version handles numeric values and another version handles strings, it is clear to the programmer what arguments to provide to call each version of the function. Overloading with unrelated datatypes is a good idea and can provide needed functionality for your application.

Problematic overloading

If different versions of a function have arguments of related datatypes (different numeric types or strings and chars), you must consider how PowerBuilder promotes datatypes in determining which function is called. This kind of overloading is undesirable because of potential confusion in determining which function is called.

When you call a function with an expression as an argument, the datatype of the expression might not be obvious. However, the datatype is important in determining what version of an overloaded function is called.

Because of the intricacies of type promotion for numeric datatypes, you might decide that you should not define overloaded functions with different numeric datatypes. Changes someone makes later can affect the application more drastically than expected if the change causes a different function to be called.

How type promotion works

When PowerBuilder evaluates an expression, it converts the datatypes of constants and variables so that it can process or combine them correctly.

Numbers

When PowerBuilder evaluates numeric expressions, it promotes the datatypes of values according to the operators and the datatypes of the other operands. For example, the datatype of the expression n/2 is double because it involves division -- the datatype of n does not matter.

Strings

When evaluating an expression that involves chars and strings, PowerBuilder promotes chars to strings.

For more information on type promotion, see Datatype of PowerBuilder expressions.

Using conversion functions

You can take control over the datatypes of expressions by calling a conversion function. The conversion function ensures that the datatype of the expression matches the function prototype you want to call.

For example, because the expression n/2 involves division, the datatype is double. However, if the function you want to call expects a long, you can use the Long function to ensure that the function call matches the prototype:

CalculateHalf(Long(n/2))

Extending and overriding events

In PowerBuilder, when you write event scripts in a descendant object, you can extend or override scripts that have been written in the ancestor. Extending (the default) means executing the ancestor's script first, then executing code in the descendant's event script.

Overriding means ignoring the ancestor's script and only executing the script in the descendant.

No overloaded events

You cannot overload an event by defining an event with the same name but different arguments. Event names must be unique.

To select extending or overriding, open the script in the Script view and check or clear the Extend Ancestor Script item in the Edit or pop-up menu.