Widening and Narrowing Conversions

A widening conversion changes a value to a data type that can hold all possible values of the source data. Widening conversions keep the source value but can change how it is stored. This occurs if you convert from an integer type to a floating-point type, or from Char to String.

A narrowing conversion changes a value to a data type that possibly cannot hold some values of the source data. For example, a value with a fraction is rounded when converted to an integer type.

Widening conversions

The table that follows shows the standard widening conversions.

Widening conversions
Data typeWidens to
Int8 Int16, Int32, Int64, Int128, Fixed64, Fixed128, Real32, Real64, Real128
UInt8 Int16, UInt16, Int32, UInt32, Int64, UInt64, Int128, UInt128, Fixed64, Fixed128, Real32, Real64, Real128
Int16 Int32, Int64, Int128, Fixed64, Fixed128, Real32, Real64, Real128
UInt16 Int32, UInt32, Int64, UInt64, Int128, UInt128, Fixed64, Fixed128, Real32, Real64, Real128
Int32 Int64, Int128, Fixed64, Fixed128, Real32, Real64, Real128
UInt32 Int64, UInt64, Int128, UInt128, Fixed64, Fixed128, Real32, Real64, Real128
Int64 Int128, Fixed64, Fixed128, Real32, Real64, Real128
UInt64 Int128, UInt128, Fixed128, Real32, Real64, Real128
Int128 Fixed128, Real32, Real64, Real128
UInt128 Real32, Real64, Real128
Fixed64 Fixed128, Real32, Real64, Real128
Fixed128 Real32, Real64, Real128
Real32 Real64, Real128
Real64 Real128
Real128 None
Char String
Char array String

Widening conversions do not cause errors at run-time.

Narrowing conversions

Narrowing conversions can cause errors at run-time. They can cause exceptions or missing data. An error occurs if the end data type cannot receive the converted value. For example, a number conversion can cause overflow. The compiler does not let you do narrowing conversions automatically.

When to use narrowing conversions

TODO

Exceptions during conversion

TODO

Reference type conversions

TODO

See also