Files
sx/examples/0182-types-cast-compound-types.sx

28 lines
1.1 KiB
Plaintext

// `cast(T) expr` accepts any compile-time-resolvable type argument,
// including compound shapes: `*T`, `[*]T`, `?T`, `[]T`. The same lowering
// makes a compound type literal a first-class `Type` value in expression
// position (`t : Type = *s64;`), mirroring named types (`t : Type = f64;`).
// Regression (issue 0118): compound casts fell into the runtime-dispatch
// path and died with "unresolved 'unknown_expr'".
#import "modules/std.sx";
main :: () {
x := 42;
p : *s64 = @x;
q : *s64 = cast(*s64) p; // no-op pointer cast
print("a: {}\n", q.*);
addr : s64 = xx p;
r : *s64 = cast(*s64) addr; // int → ptr through compound cast
print("b: {}\n", r.*);
mp : [*]s64 = cast([*]s64) p; // ptr → many-pointer
print("c: {}\n", mp[0]);
o : ?s32 = cast(?s32) 7; // optional wrap
print("d: {}\n", o ?? -1);
arr := .[1, 2, 3];
s : []s64 = cast([]s64) arr; // array → slice
print("e: {} {}\n", s.len, s[2]);
t : Type = *s64; // first-class compound Type value
print("f: {}\n", type_name(t));
}