Files
sx/examples/20-any-varargs.sx
2026-02-09 18:07:41 +02:00

48 lines
1.0 KiB
Plaintext

#import "modules/std.sx";
Point :: struct {
x: s32;
y: s32;
}
// Print all arguments — accepts any type, dispatches via type-switch
print_any :: (args: ..Any) {
for args {
type := type_of(it);
if type == {
case int: write(int_to_string(cast(s32) it));
case string: write(cast(string) it);
case bool: write(bool_to_string(cast(bool) it));
case float: write(float_to_string(cast(f64) it));
case Point: {
p := cast(Point) it;
write("(");
write(int_to_string(p.x));
write(",");
write(int_to_string(p.y));
write(")");
}
}
write(" ");
}
write("\n");
}
count :: (args: ..Any) -> s32 {
args.len;
}
main :: () -> s32 {
print_any(42, "hello", true, 3.14);
// Test with struct
p := Point.{ x=10, y=20 };
print_any("point:", p, 99);
// Test count
write(int_to_string(count(1, 2, 3)));
write("\n");
0;
}