cast
Builtin index · Collections & strings
result = cast(x, type)
Input: x, type · Output: result
What it is
x is any value; type is a Str naming the target: num, bool, str, vec, mat, complex, cvec, cmat, signal, mask, table, model, list, record, image, timer, file, worker, mutex, semaphore, none.
Overloads
One call shape — this function is not overloaded.
| Code | Input | Output | Description |
result = cast(x, type) | x —x is any value.type Str or booltype is a Str naming the target: num, bool, str, vec, mat, complex, cvec, cmat, signal, mask, table, model, list, record, image, timer, file, worker, mutex, semaphore, none. | result — | Returns a value of that kind. A conversion that is not defined is an error, not a best effort. |
Examples
From the documentation, and run by the build -- the output and the figure below are what the engine actually produced. Simplest first.
n = cast(("1", "2.5", "-3"), "num")
print(n) # [1, 2.5, -3]
s = cast([1, 2.5, -3], "str")
print(s) # ("1", "2.5", "-3")
b = "42".cast("num")
print(b) # 42, via method-chain sugar
[1, 2.5, -3]
("1", "2.5", "-3")
42from collections-strings
How it works
cast(value, tag) — explicit type conversion between real Qu value kinds, using the SAME closed tag vocabulary (qu_syntax::TYPE_TAGS) typed function parameters already use (num, bool, str, vec, ...) rather than a parallel naming scheme. Deliberately distinct from x as vector(3) (Expr::As/apply_shape_contract), which is a shape *contract* check on an already-typed value, not a type conversion — the two features solve unrelated problems and must not collide. Only the pairings below are wired up (most of the 21×21 possible (source, target) combinations are meaningless, e.g. casting a signal to a worker); anything else is a clear named error, never a silent no-op or a panic. Automatically gets x.cast(tag) method-chain syntax for free via the existing generic recv.method(args) == method(recv, args) sugar (eval_call) — no extra wiring needed here for that.
From the note above this builtin in the interpreter, where the reasoning was written down as it was built.