Files
sx/examples/protocols/0400-protocols-impl-for-builtin.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

31 lines
799 B
Plaintext

// impl Protocol for built-in scalar types (f32, i64, bool, u32, ...) —
// both static dispatch (`f32.lerp(...)`) and protocol-boxed dispatch via
// `#inline` erasure.
Lerpable :: protocol #inline {
lerp :: (self: *Self, b: Self, t: f32) -> Self;
}
impl Lerpable for f32 {
lerp :: (self: f32, b: f32, t: f32) -> f32 { self + (b - self) * t }
}
do_lerp :: (a: Lerpable, b: f32, t: f32) -> f32 {
a.lerp(b, t)
}
main :: () -> void {
// Static call through impl
result := f32.lerp(0.0, 10.0, 0.5);
print("lerp(0, 10, 0.5) = {}\n", result);
// Protocol dispatch through #inline erasure
val : f32 = 0.0;
p : *f32 = @val;
l : Lerpable = xx p;
result2 := do_lerp(l, 10.0, 0.25);
print("lerp(0, 10, 0.25) = {}\n", result2);
}
#import "modules/std.sx";