Type Operators

is type

Type checking operator.

type | type

Type union operator.

<type> val

Type cast operator.

typeof anytype

Static type inference operator.

introspect type

Static type introspection operator.

operator

is type

Types - 图1

Types - 图2

Types - 图3

anytype is type -> boolanytype is not type -> bool

Type checking operator.

Check if A is an instance of B or any of B’s subtypes.

Type-checking operators is and is not that test whether the left operand is of any of the types given by the comma-separated list of types provided as the right operand.

Note that B is special and is not any kind of expression, so it does not in any way participate in the interactions of sets and longest common prefix rules.

  1. db>
  1. select 1 is int64;
  1. {true}
  1. db>
  2. ...
  1. select User is not SystemUser
  2. filter User.name = 'Alice';
  1. {true}
  1. db>
  1. select User is (Text | Named);
  1. {true, ..., true} # one for every user instance

operator

type | type

Types - 图4

Types - 图5

Types - 图6

type | type -> type

Type union operator.

This operator is only valid in contexts where type checking is done. The most obvious use case is with the is and is not. The operator allows to refer to a union of types in order to check whether a value is of any of these types.

  1. db>
  1. select User is (Text | Named);
  1. {true, ..., true} # one for every user instance

It can similarly be used when specifying a link target type. The same logic then applies: in order to be a valid link target the object must satisfy object is (A | B | C).

  1. abstract type Named {
  2. required property name -> str;
  3. }
  4. abstract type Text {
  5. required property body -> str;
  6. }
  7. type Item extending Named;
  8. type Note extending Text;
  9. type User extending Named {
  10. multi link stuff -> Named | Text;
  11. }

With the above schema, the following would be valid:

  1. db>
  1. insert Item {name := 'cube'};
  1. {Object { id: <uuid>'...' }}
  1. db>
  1. insert Note {body := 'some reminder'};
  1. {Object { id: <uuid>'...' }}
  1. db>
  2. ...
  3. ...
  4. ...
  1. insert User {
  2. name := 'Alice',
  3. stuff := Note, # all the notes
  4. };
  1. {Object { id: <uuid>'...' }}
  1. db>
  2. ...
  3. ...
  4. ...
  1. insert User {
  2. name := 'Bob',
  3. stuff := Item, # all the items
  4. };
  1. {Object { id: <uuid>'...' }}
  1. db>
  2. ...
  3. ...
  4. ...
  5. ...
  6. ...
  7. ...
  1. select User {
  2. name,
  3. stuff: {
  4. [is Named].name,
  5. [is Text].body
  6. }
  7. };
  1. {
  2. Object {
  3. name: 'Alice',
  4. stuff: {Object { name: {}, body: 'some reminder' }}
  5. },
  6. Object {
  7. name: 'Bob',
  8. stuff: {Object { name: 'cube', body: {} }}
  9. }
  10. }

operator

<type> val

Types - 图7

Types - 图8

Types - 图9

< type > anytype -> anytype

Type cast operator.

A type cast operator 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.

Type cast is a run-time operation. The cast will succeed only if a type conversion was defined for the type pair, and if the source value satisfies the requirements of a target type. EdgeDB allows casting any scalar.

It is illegal to cast one Object into another. The only way to construct a new Object is by using insert. However, the type intersection can be used to achieve an effect similar to casting for Objects.

When a cast is applied to an expression of a known type, it represents a run-time type conversion. The cast will succeed only if a suitable type conversion operation has been defined.

Examples:

  1. db>
  2. ...
  1. # cast a string literal into an integer
  2. select <int64>"42";
  1. {42}
  1. db>
  2. ...
  1. # cast an array of integers into an array of str
  2. select <array<str>>[1, 2, 3];
  1. {['1', '2', '3']}
  1. db>
  2. ...
  1. # cast an issue number into a string
  2. select <str>example::Issue.number;
  1. {'142'}

Casts also work for converting tuples or declaring different tuple element names for convenience.

  1. db>
  1. select <tuple<int64, str>>(1, 3);
  1. {[1, '3']}
  1. db>
  2. ...
  3. ...
  4. ...
  5. ...
  6. ...
  7. ...
  8. ...
  1. with
  2. # a test tuple set, that could be a result of
  3. # some other computation
  4. stuff := (1, 'foo', 42)
  5. select (
  6. # cast the tuple into something more convenient
  7. <tuple<a: int64, name: str, b: int64>>stuff
  8. ).name; # access the 'name' element
  1. {'foo'}

An important use of casting is in defining the type of an empty set {}, which can be required for purposes of type disambiguation.

  1. with module example
  2. select Text {
  3. name :=
  4. Text[is Issue].name IF Text is Issue ELSE
  5. <str>{},
  6. # the cast to str is necessary here, because
  7. # the type of the computed expression must be defined
  8. body,
  9. };

Casting empty sets is also the only situation where casting into an Object is valid:

  1. with module example
  2. select User {
  3. name,
  4. friends := <User>{}
  5. # the cast is the only way to indicate that the
  6. # computed link 'friends' is supposed to refer to
  7. # a set of Users
  8. };

For more information about casting between different types consult the casting table.

operator

typeof anytype

Types - 图10

Types - 图11

Types - 图12

typeof anytype -> type

Static type inference operator.

This operator converts an expression into a type, which can be used with is, is not, and introspect.

Currently, typeof operator only supports scalars and objects, but not the collections as a valid operand.

Consider the following types using links and properties with names that don’t indicate their respective target types:

  1. type Foo {
  2. property bar -> int16;
  3. link baz -> Bar;
  4. }
  5. type Bar extending Foo;

We can use typeof to determine if certain expression has the same type as the property bar:

  1. db>
  1. insert Foo { bar := 1 };
  1. {Object { id: <uuid>'...' }}
  1. db>
  1. select (Foo.bar / 2) is typeof Foo.bar;
  1. {false}

To determine the actual resulting type of an expression we can use introspect:

  1. db>
  1. select introspect (typeof Foo.bar).name;
  1. {'std::int16'}
  1. db>
  1. select introspect (typeof (Foo.bar / 2)).name;
  1. {'std::float64'}

Similarly, we can use typeof to discriminate between the different Foo objects that can and cannot be targets of link baz:

  1. db>
  1. insert Bar { bar := 2 };
  1. {Object { id: <uuid>'...' }}
  1. db>
  2. ...
  3. ...
  4. ...
  1. select Foo {
  2. bar,
  3. can_be_baz := Foo is typeof Foo.baz
  4. };
  1. {
  2. Object { bar: 1, can_be_baz: false },
  3. Object { bar: 2, can_be_baz: true }
  4. }

operator

introspect type

Types - 图13

Types - 图14

Types - 图15

introspect type -> schema::Type

Static type introspection operator.

This operator returns the introspection type corresponding to type provided as operand. It works well in combination with typeof.

Currently, the introspect operator only supports scalar types and object types, but not the collection types as a valid operand.

Consider the following types using links and properties with names that don’t indicate their respective target types:

  1. type Foo {
  2. property bar -> int16;
  3. link baz -> Bar;
  4. }
  5. type Bar extending Foo;
  1. db>
  1. select (introspect int16).name;
  1. {'std::int16'}
  1. db>
  1. select (introspect Foo).name;
  1. {'default::Foo'}
  1. db>
  1. select (introspect typeof Foo.bar).name;
  1. {'std::int16'}

For any object type SomeType the expressions introspect SomeType and introspect typeof SomeType are equivalent as the object type name is syntactically identical to the expression denoting the set of those objects.

There’s an important difference between the combination of introspect typeof SomeType and SomeType.__type__ expressions when used with objects. introspect typeof SomeType is statically evaluated and does not take in consideration the actual objects contained in the SomeType set. Conversely, SomeType.__type__ is the actual set of all the types reachable from all the SomeType objects. Due to inheritance statically inferred types and actual types may not be the same (although the actual types will always be a subtype of the statically inferred types):

  1. db>
  2. ...
  1. # first let's make sure we don't have any Foo objects
  2. delete Foo;
  1. { there may be some deleted objects here }
  1. db>
  1. select (introspect typeof Foo).name;
  1. {'default::Foo'}
  1. db>
  1. select Foo.__type__.name;
  1. {}
  1. db>
  2. ...
  1. # let's add an object of type Foo
  2. insert Foo;
  1. {Object { id: <uuid>'...' }}
  1. db>
  2. ...
  1. # Bar is also of type Foo
  2. insert Bar;
  1. {Object { id: <uuid>'...' }}
  1. db>
  1. select (introspect typeof Foo).name;
  1. {'default::Foo'}
  1. db>
  1. select Foo.__type__.name;
  1. {'default::Bar', 'default::Foo'}