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#
stringOptionfn: 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 bynew.static: Declares a module-level or class-level slot shared by the whole program, or a method withoutself.readonly: A read-only view of a value: nothing reachable through it can be changed.if: Runs a block when aboolcondition is true.