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 PocketBuilder 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 PocketBuilder evaluates an expression, it converts the datatypes of constants and variables so that it can process or combine them correctly.

Numbers When PocketBuilder 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, PocketBuilder promotes chars to strings.

For more information on type promotion, see “Datatype of PocketBuilder 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))