Conversions¶
An explicit conversion uses AS, optionally followed by a conversion mode:
VAR wide I64 := 300;
VAR narrowed I8 := wide AS PARTIAL I8;
VAR checked I32 := wide AS CHECKED I32;
VAR assumed I32 := wide AS ASSUME I32;
VAR approximate F32 := 0.4 AS APPROXIMATE F32;
Modes¶
| Form | Contract |
|---|---|
expression AS Type |
Ordinary explicit conversion with no extra permission |
AS EXPLICIT Type |
Selects an explicit user conversion category |
AS PARTIAL Type |
Permits a partial representation result, such as discarded high bits |
AS CHECKED Type |
Requires validity to be checked and faults on failure |
AS ASSUME Type |
Makes validity a program precondition |
AS APPROXIMATE Type |
Permits an inexact numeric result |
AS REINTERPRET Type |
Selects a representation-level conversion explicitly allowed by the type system |
AS UNCHECKED_STATIC_DOWNCAST PointerOrReferenceType |
Recovers an exact complete struct type from an unambiguous base; no runtime check |
AS DYNAMIC PointerType |
Performs a checked polymorphic downcast or cross-cast |
Narrowing¶
Narrowing is not silently accepted. The programmer selects whether truncation, a dynamic check, an assumption, or approximation matches the intended semantics.
Pointer reinterpretation¶
Current pointer reinterpretation supports explicit paths such as conversion
through VOID while preserving pointer category and validity constraints:
VAR pointer CONST->I32 := value<-;
VAR erased CONST->VOID := pointer AS REINTERPRET CONST->VOID;
VAR restored CONST->I32 := erased AS REINTERPRET CONST->I32;
REINTERPRET does not establish an unrelated live type in storage.
Typed Storage and Explicit Lifetime specifies
lifetime transitions.
User-defined conversions¶
Constructor parameter names select the corresponding conversion categories,
including @OTHER, @EXPLICIT, @PARTIAL, @CHECKED, @ASSUME,
@APPROXIMATE, and @REINTERPRET. See
Constructors and destructors.
AS DYNAMIC is restricted to polymorphic instance pointers and returns null
when no unique target subobject exists. See Inheritance for
qualification, RTTI, and target-support rules.
AS UNCHECKED_STATIC_DOWNCAST is a built-in operation for instance pointers
and references, including nonpolymorphic structs. It cannot be overloaded.
The complete type must exactly match the destination, and repeated nonvirtual
source bases cause a compilation error. See
Unchecked static downcasts for
null handling, qualification, virtual bases, and undefined behavior.