Files
sx/examples/optionals/0905-optionals-unwrap-field-chain.sx
agra 66bdc70bf1 test: group examples into per-category folders
Move examples/*.sx and their expected/ snapshots into per-category
subfolders (examples/<category>/...). Folder = leading filename token,
with ffi-objc/ffi-jni kept whole; filenames are unchanged. The corpus
runner and LSP sweep now discover each category's expected/ dir, while
issues/ stays flat. Example 1058's repo-root-relative companion import
is made file-relative. Path strings embedded in 164 snapshots were
regenerated (path-only changes). Test-layout docs in CLAUDE.md updated.
2026-06-21 14:41:34 +03:00

52 lines
1.6 KiB
Plaintext

// Postfix `!` (optional force-unwrap) chained directly with a member access.
// `opt!.field`, `opt!.method()`, `opt!.a.b`, and `opt![i]` must read the same
// value the bind-first form (`v := opt!; v.field`) produces — the unwrapped
// value's type has to flow into the chained access.
//
// Regression (issue 0101): chained `opt!.field` typed its receiver as
// `.unresolved` (inferExprType had no force_unwrap arm), so a string field read
// as garbage and `opt!.method()` failed to resolve at all.
#import "modules/std.sx";
Inner :: struct { tag: string; k: i64; }
S :: struct {
id: string;
n: i64;
inner: Inner;
greet :: (self: *S) -> string { return self.id; } // pointer receiver
bump :: (self: S, extra: i64) -> i64 { return self.n + extra; } // value receiver
}
mk :: () -> ?S {
return S.{ id = "hello", n = 42, inner = Inner.{ tag = "deep", k = 7 } };
}
arr :: () -> ?[3]i64 {
v : [3]i64 = .[10, 20, 30];
return v;
}
main :: () -> void {
// opt!.field — string and int field, chained vs bind-first.
print("chain id: {}\n", mk()!.id); // hello
print("chain n: {}\n", mk()!.n); // 42
v := mk()!;
print("bind id: {}\n", v.id); // hello
print("bind n: {}\n", v.n); // 42
// opt!.method()
print("meth ptr: {}\n", mk()!.greet()); // hello
print("meth val: {}\n", mk()!.bump(8)); // 50
// nested opt!.a.b
print("nest tag: {}\n", mk()!.inner.tag); // deep
print("nest k: {}\n", mk()!.inner.k); // 7
// opt![i]
print("index 0: {}\n", arr()![0]); // 10
print("index 2: {}\n", arr()![2]); // 30
}