I think this could be better now:
// if b is not given, use 0 instead
fn myfn(a: i32, b: i32 = 0) {
a + b
}
myfn(1); //> 1
myfn(1, 2); //> 3
// Option<T> will be used for every argument
// that has no default value
fn myfn2(a: i32, b?: i32) {
a + b.unwrap_or(0)
}
myfn2(1); //> 1
myfn2(1, 2); //> 3
// For optional arguments inbetween
fn myfn3(a: i32, b: i32 = 0, c: i32 = 0, d: i32) {
a + b + c + d
}
myfn3(1,,, 2); //> 3
myfn3(1, 2,, 3); //> 6
myfn3(1, 2, 3, 4); //> 10
And this would compile to something like this:
fn myfn(a: i32, b: Option<i32>) {
let b = b.unwrap_or(0);
a + b
}
myfn(1, None);
myfn(1, Some(2));
fn myfn2(a: i32, b: Option<i32>) {
a + b.unwrap_or(0)
}
myfn2(1, None);
myfn2(1, Some(2));
fn myfn3(a: i32, b: Option<i32>, c: Option<i32>, d: i32) {
let b = b.unwrap_or(0);
let c = c.unwrap_or(0);
a + b + c + d
}
myfn3(1, None, None, 2);
myfn3(1, Some(2), None, 3);
myfn3(1, Some(2), Some(3), 4);
I think something like this shouldn't be that hard to implement as it's just a preprocessing operation.
With this approach, we wouldn't have problems with optional positional arguments.