as operator.
Non-assignable types give a compilation error
Assignable types may be passed as arguments, assigned to fields, etc. This behavior follows conventional type-system rules.int to intN, AliasForT and T, Cell<T> to cell.
as may be used to override the restriction.
Unsafe operator as
For example, bool is not assignable to int, but a boolean is backed by TVM INT (-1 or 0), making the cast valid:
as can be used to convert types that have equal TVM representation.
Using as explicitly forces a developer to verify that the transformation is correct.
No additional runtime instructions are inserted — it is purely a type cast.
Some examples:
addressis TVM SLICE, sosomeAddr as sliceis valid- conversely,
someSlice as address bits123is TVM SLICE, sosomeSlice as bits123is validintNis TVM INT, sosomeInt64 as int32is validboolis TVM INT, sosomeBool as intandsomeBool as int32are valid, resulting in -1 or 0- an enum is TVM INT, so
someColor as intis valid - and similar conversions apply.
as is appropriate, the compiler indicates this in diagnostic messages, as shown above.
Not all transformations are possible.
For example, someInt as cell is invalid.
Operator `as` is unsafeThe
as operator locally skips typechecking and does no validation at runtime.
For example, someSlice as address — if someSlice holds invalid address,
the program execution may become undefined.Operator ! (non-null assertion)
The ! operator is similar to ! in TypeScript and !! in Kotlin. It allows bypassing the compiler’s nullability check:
Smart casts
Once a variable is checked, the compiler automatically narrows its type.Operator is for union types
Given a union type T1 | T2 | ..., the operator is performs a runtime type test.
as operator does not apply to unions: v as Point is incorrect. Use is and smart casts.
Operators is, !is, and match are described in union types.