Skip to main content

Value Casting

A value cast converts a numeric value’s type (uintN, intN, floatN). For example, you can turn a uint16 value into an int32 value. Value casts can be defined in Flex transforms and functions, and there are a few methods available for handling different typecases. There is a built-in method float64to32 for handling that particular typecase, a total cast value_cast for typecases that are not lossy, and a partial cast value_cast? for handling typecases that may be lossy.

float64to32

Use float64to32 if you need to convert from float64 to float32. It will always succeed and return a value. Note, casting of this kind may be lossy; you may lose floating point precision.

Syntax

flex
float64to32(EXP)

Casts the float64 expression EXP to float32.

Example

flex
message struct A { milliseconds : float64;}
message struct B { seconds : float32;}
transform AtoB(a : A) -> B {
B { seconds = float64to32(a.milliseconds / 1000.0); };
// This converts the float64 expression a.milliseconds / 1000.0 to a float32 value.
}

value_cast

Use value_cast for typecases where a cast will not result in a loss of information (e.g. converting from a smaller integer type to a larger one. Supported typecases are defined below). If your value_cast expression typechecks successfully in the Flex editor, then it is guaranteed to return a value and no information will be lost as a result of the cast.

Syntax

flex
value_cast<TYPE>(EXP)

Casts the numeric expression EXP to TYPE.

Defined for:

  • uintN to larger uintN
  • uintN to larger intN
  • intN to larger intN
  • float32 to float64
  • Integers (uintN or intN) that are smaller than the integral part of a float to float32 or float64

Example

flex
value_cast<int32>(a : int16) // returns a : int32
value_cast<uint16>(b : uint8) // returns b : uint16
value_cast<float64>(c : float32) // returns c : float64
// The following are invalid:
value_cast<int32>(d : int64) // invalid because it's going to a smaller integer
value_cast<int64>(e : float32) // invalid because float to integer is not supported
value_cast<uint8>(f : bit) // invalid because bit types are not supported

value_cast?

Use value_cast? for typecases where a cast may result in a loss of information. It will always result in an Optional type (e.g. <Optional>int32). A value is only returned if the cast was not lossy. If information was lost, None is returned instead.

Syntax

flex
value_cast?<TYPE>(EXP)

Casts the numeric expression EXP to Optional<TYPE>.

Defined for:

  • Everything that value_cast is defined for
  • Any integer (intN or uintN) to any other integer (intN or uintN)
  • float64 to float32

Example

flex
value_cast?<int16>(a : int32) // returns a : Optional<int16>
value_cast?<int8>(b : uint16) // returns b : Optional<int8>
value_cast?<float32>(c : float64) // returns c : Optional<float32>

getOrElse

Use the built-in method getOrElse together with value_cast? to return a default value instead of None if the result of the cast is lossy.

Syntax

flex
getOrElse(value_cast?<TYPE>(EXP), DEFAULT)

Casts the numeric expression EXP to TYPE, and returns DEFAULT if the cast is lossy.

Example

flex
message struct A { milliseconds : int64;}
message struct B { seconds : float64;}
transform AtoB(a : A) -> B {
B { seconds = getOrElse(value_cast?<float64>(a.milliseconds), 0.0) / 1000.0; };
// If the value cast returns `None`, the default value `0.0` is used instead
}