Optional function arguments

Thank you.

I think this is this is the core Question that needs to be answered first (if the goal is to have optional arguments of any kind), as it's one of (if not the) most fundamental part of functions. So far I haven't seen much discussion regarding this. Most of the discussion has been around syntax.

My answer to this would be:

  • Compile the function as if all arguments are required and provided by the caller
  • For fn foo(u32, u32) and impl fn foo(u32, u32): Use the function directly
  • For fn(u32) use an wrapper that adds the default (thus the call-side doesn't need to be aware of optional arguments), it should probably be reused between all calls using this to reduce load on LLVM.
  • For impl Fn(u32) we can also use the (#[inline]) wrapper (probably easier to implement than manually ensuring the second argument is there). LLVM will most likely inline our function into the wrapper or the wrapper into the function.
// Or whatever syntax to denote something as optional
fn foo(a: u32, b: u32 = 0) {}

// Added by the compiler when used as a `fn(u32)` or `impl Fn(u32)`
fn foo__1(a: u32) {
    foo(a, 0)
}