Keyword auto-imported

typeof

Source: compiler/src/builtin_docs/keywords.md

Gives the static type of an expression as a string, at compile time.

The operand is type-checked but never evaluated. Nominal types are written with their defining module (prelude::Option<int>); types from the entry file stay bare. A type that is not fully known is a compile error.

typeof is a unary prefix, like - or readonly: it takes the operand right after it (with any calls, fields or indexing), so typeof x == "int" compares the type name. Parenthesize a larger operand: typeof (a + b).

fn main() {
    let a = typeof 42; // "int"
    let b = typeof (1, "x"); // "(int, string)"
    let c = typeof Option::Some(1); // "prelude::Option<int>"
    let d = typeof a == "string"; // true
}

See also#

  • string
  • Option
  • fn: Declares a function, a method, or (written in an expression) an anonymous function.
  • let: Declares a mutable local variable.
  • const: Declares a binding that cannot be reassigned: a local, a module constant, or a class field that is set only by new.
  • static: Declares a module-level or class-level slot shared by the whole program, or a method without self.
  • readonly: A read-only view of a value: nothing reachable through it can be changed.
  • if: Runs a block when a bool condition is true.
↑ ↓ navigate ↵ open