32 lines
873 B
Plaintext
32 lines
873 B
Plaintext
// issue-0002: impl for built-in types fails with "expected type name after 'for'"
|
|
//
|
|
// `impl Protocol for f32` should work the same as `impl Protocol for MyStruct`.
|
|
// Currently the parser rejects built-in type names (f32, s64, bool, etc.) after `for`.
|
|
|
|
Lerpable :: protocol #inline {
|
|
lerp :: (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";
|