so... jai :D
This commit is contained in:
47
examples/20-any-varargs.sx
Normal file
47
examples/20-any-varargs.sx
Normal file
@@ -0,0 +1,47 @@
|
||||
#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;
|
||||
}
|
||||
Reference in New Issue
Block a user