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:
1 |
CalculateHalf(Long(n/2)) |