as operator.
Automatic conversions
Assignable types can be provided explicitly:inttointN.AliasForTandT(bidirectional).Cell<T>tocell.
Smart casts
Once a variable is checked, the compiler narrows its type. For instance, if a variable could benull before the check, and the condition is confirmed to be true, the variable’s type will not allow null in the true block anymore.
Operator is for union types
Given a union type T1 | T2 | ..., the is operator performs a runtime type test, narrowing the type.
Non-null assertion with operator !
The ! operator bypasses the compiler’s nullability check. It is similar to ! in TypeScript and !! in Kotlin.
Unsafe cast with operator as
When non-trivial type transformations are required, the as operator overrides the restrictions by performing an unsafe type cast.
For instance, bool cannot be assigned directly to int. However, a direct cast using the as operator is valid since booleans are represented as TVM integers: true is -1, and false is 0.
The
as operator skips type checking and performs no validation at runtime — it is purely a compile-time type cast.This can lead to sudden errors and hidden bugs. For example, if one cast a slice as address and that slice did not hold a valid address, subsequent program becomes undefined and may error whenever that new “address” is used.42 as cell is invalid. When a cast is inappropriate, the compiler indicates this in diagnostic messages.
The as operator converts types that share the same TVM representation:
addressis a TVM slice, sosomeAddr as sliceis valid.- Conversely,
someSlice as addressis valid.
- Conversely,
bitsN, whereNis between 1 and 1023, is a TVM slice, sosomeSlice as bitsNis valid.intN, whereNis between 1 and 257, is a TVM integer, sosomeInt64 as int32is valid.boolis as TVM integer, sosomeBool as intandsomeBool as int32result in -1 or 0.- Enums are TVM integers, so
someColor as intis valid.
as operator cannot be applied to unions: v as Point is incorrect. Use the is operator and smart casts.