Revision [21415]

This is an old revision of ProPgDataConversion made by fxm on 2016-04-24 08:31:18.


Coercion and Conversion

Transfer the data of a given type variable to a variable of another type requires conversion.

Coercion of Numeric Data Types in Expressions

When two different data types are used in a binary operation, like + (Addition) or = (Assignment), the smaller data type is automatically promoted to the larger data type regardless of the order in which the arguments are given.

Promotions are as follows:
All unsigned integer types are handling like signed integer types for the purpose of promotion, and the most significant bit is extended (sign extension).

Conversion of Numeric Data Types

A type conversion will occur implicitly when an expression or variable is assigned, passed as a parameter to a procedure, or returned as a result from a procedure. Conversions may also be explicit when using CAST or one of the built-in conversion functions (among standard functions).

Integer To Integer, any combination of Signed and Unsigned
Integer to Single or Double
Double to Single
Double or Single to Integer
Conversions using User Data Type constructors and operators

For conversion between built-in types (among standard types like between numeric types as above or between string types), the compiler knows what to do without the need for instructions from user.
This is called the implicit internal conversion (or coercion).

When one of the two types is at least a UDT (User Defined Type), the user has to code some UDT procedures to define how do the conversion.
Then, the conversion execution can be explicit if the user specifies what UDT procedure must be used, or implicit if the user leaves the choice to compiler.

In the world of UDTs, conversions can be controlled by means of three member procedures:
For construction with implicit initialization, the compiler searches:
For implicit assignment, the compiler searches:
(a matched constructor is not searched by compiler on an implicit assignment)

Very simple syntaxic example highlighting the conversion capabilities by using Cast operator (explicit and implicit), Constructor (explicit and implicit) and Let operator (implicit):
Type UDT
  Dim As Integer I
  Declare Operator Cast () As Integer
  Declare Constructor (ByVal I0 As Integer)
  Declare Operator Let (ByVal I0 As Integer)
End Type

Operator UDT.Cast () As Integer
  Return This.I
End Operator

Constructor UDT (ByVal I0 As Integer)
  This.I = I0
End Constructor

Operator UDT.Let (ByVal I0 As Integer)
  This.I = I0
End Operator

Dim As Integer J = 12
Dim As UDT u1 = UDT(J)  '' construction with explicit initialization using the defined "Constructor(Byval As Integer)" operator
Print u1.I
Dim As UDT u2 = J       '' construction with implicit initialization by compiler using the defined "Constructor(Byval As Integer)" operator
Print u2.I

u1.I = 34
J = Cast(Integer, u1)  '' explicit assignment using the defined "Cast() As Integer" operator
Print J
Dim As Integer K
K = u1                 '' implicit assignment by compiler using the defined "Cast() As Integer" operator
Print K

J = 56
u1 = J
Print u1.I             '' implicit assignment by compiler using the defined "Let(Byval As Integer)" operator


See also:
Back to Programmer's Guide
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki phatcode