Casts

There are different ways that casts appear in EdgeQL.

Explicit Casts

A type cast expression converts the specified value to another value of the specified type:

  1. "<" type ">" expression

The type must be a valid type expression denoting a non-abstract scalar or a container type.

For example, the following expression casts an integer value into a string:

  1. db>
  1. select <str>10;
  1. {"10"}

See the type cast operator section for more information on type casting rules.

Assignment Casts

Assignment casts happen when inserting new objects. Numeric types will often be automatically cast into the specific type corresponding to the property they are assigned to. This is to avoid extra typing when dealing with numeric value using fewer bits:

  1. # Automatically cast a literal 42 (which is int64
  2. # by default) into an int16 value.
  3. insert MyObject {
  4. int16_val := 42
  5. };

If assignment casting is supported for a given pair of types, explicit casting of those types is also supported.

Implicit Casts

Implicit casts happen automatically whenever the value type doesn’t match the expected type in an expression. This is mostly supported for numeric casts that don’t incur any potential information loss (in form of truncation), so typically from a less precise type, to a more precise one. The int64 to float64 is a notable exception, which can suffer from truncation of significant digits for very large integer values. There are a few scenarios when implicit casts can occur:

  1. Passing arguments that don’t match exactly the types in the function signature:
  1. ```
  2. db>
  3. ...
  4. ```
  5. ```
  6. with x := <float32>12.34
  7. select math::ceil(x);
  8. ```
  9. ```
  10. {13}
  11. ```
  12. The function [math::ceil()]($4f9505f0f3fe6a08.md#function::math::ceil) only takes [int64]($e4914287563ac31b.md#type::std::int64), [float64]($e4914287563ac31b.md#type::std::float64), [bigint]($e4914287563ac31b.md#type::std::bigint), or [decimal]($e4914287563ac31b.md#type::std::decimal) as its argument. So the [float32]($e4914287563ac31b.md#type::std::float32) value will be *implicitly cast* into a [float64]($e4914287563ac31b.md#type::std::float64) in order to match a valid signature.
  1. Using operands that don’t match exactly the types in the operator signature (this works the same way as for functions):
  1. ```
  2. db>
  3. ```
  4. ```
  5. select 1 + 2.3;
  6. ```
  7. ```
  8. {3.3}
  9. ```
  10. The operator [+]($e4914287563ac31b.md#operator::plus) is defined only for operands of the same type, so in the expression above the [int64]($e4914287563ac31b.md#type::std::int64) value `1` is *implicitly cast* into a [float64]($e4914287563ac31b.md#type::std::float64) in order to match the other operand and produce a valid signature.
  1. Mixing different numeric types in a set:
  1. ```
  2. db>
  3. ```
  4. ```
  5. select {1, 2.3, <float32>4.5} is float64;
  6. ```
  7. ```
  8. {true, true, true}
  9. ```
  10. All elements in a set have to be of the same type, so the values are cast into [float64]($e4914287563ac31b.md#type::std::float64) as that happens to be the common type to which all the set elements can be *implicitly cast*. This would work out the same way if [union]($f4a08709faa70d7b.md#operator::union) was used instead:
  11. ```
  12. db>
  13. ```
  14. ```
  15. select (1 union 2.3 union <float32>4.5) is float64;
  16. ```
  17. ```
  18. {true, true, true}
  19. ```

If implicit casting is supported for a given pair of types, assignment and explicit casting of those types is also supported.

Casting Table

from \ to

json

str

float32

float64

int16

int32

int64

bigint

decimal

bool

bytes

uuid

datetime

duration

local_date

local_datetime

local_time

json

<>

<>

<>

<>

<>

<>

<>

<>

<>

<>

<>

<>

<>

<>

<>

str

<>

<>

<>

<>

<>

<>

<>

<>

<>

<>

<>

<>

<>

<>

<>

float32

<>

<>

impl

<>

<>

<>

<>

<>

float64

<>

<>

:=

<>

<>

<>

<>

<>

int16

<>

<>

impl

impl

impl

impl

impl

impl

int32

<>

<>

impl

<>

impl

impl

impl

int64

<>

<>

:=

impl

:=

:=

impl

impl

bigint

impl

decimal

<>

<>

<>

<>

<>

<>

<>

<>

bool

<>

<>

bytes

uuid

<>

<>

datetime

<>

<>

duration

<>

<>

local_date

<>

<>

<>

local_datetime

<>

<>

<>

<>

local_time

<>

<>

  • <> - can be cast explicitly

  • := - assignment cast is supported

  • “impl” - implicit cast is supported