-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathvariadic.alu
43 lines (35 loc) · 1.09 KB
/
variadic.alu
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
use std::builtins::Tuple;
use std::typing::{type_name, is_void};
// #[tuple_args] attribute is used to make the function receive its arguments as a single tuple
// instead of multiple arguments.
#[tuple_args]
fn print_debug<T: Tuple>(args: T) {
// We can use recursion to go through all the elements...
when args.len() == 0 {
println!("");
} else when args.len() == 1 {
println!("[{}] {}", type_name::<T.0>(), args.0);
} else {
print!("[{}] {}, ", type_name::<T.0>(), args.0);
// invoke is the converse of #[tuple_args], it allows us to call a function with a
// tuple of arguments.
(print_debug::<T.(1..)>).invoke(args.(1..));
}
}
#[tuple_args]
fn make_array<T: Tuple + !()>(t: T) -> [T.0; t.len()] {
// ...but we don't have to, we can use a const loop instead!
let arr: [T.0; t.len()];
for const i in 0usize..t.len() {
arr[i] = t.(i);
}
arr
}
fn main() {
print_debug();
print_debug(1);
print_debug(1, "hello");
print_debug(1, "hello", true);
const ARR = make_array(1, 2, 3);
println!("{}, {}, {}", ARR[0], ARR[1], ARR[2]);
}