Files
sx/examples/issue-0010.sx
agra dd14f1206b ir
2026-02-26 02:25:02 +02:00

39 lines
1.0 KiB
Plaintext

// Issue 0010: inline if-else in struct literal field produces type error
// The `null` branch is typed as `*void` instead of being coerced to `?f32`
//
// Error: narrowing conversion from '*void' to 'f32' requires explicit 'xx' cast
#import "modules/std.sx";
Foo :: struct {
width: ?f32;
}
main :: () -> void {
x :f32: 10.0;
// null in then branch, value in else
f1 := Foo.{ width = if true then null else x };
print("{}\n", f1.width ?? 99.0);
// value in then branch, null in else
f2 := Foo.{ width = if true then x else null };
print("{}\n", f2.width ?? 99.0);
// both branches are values
f3 := Foo.{ width = if false then 5.0 else x };
print("{}\n", f3.width ?? 99.0);
// standalone variable, not just struct fields
val: ?f32 = if true then null else 42.0;
print("{}\n", val ?? 0.0);
val2: ?f32 = if false then null else 42.0;
print("{}\n", val2 ?? 0.0);
// negation in condition
cond := false;
val3: ?f32 = if !cond then null else 42.0;
print("{}\n", val3 ?? 0.0);
}