How would you overload with a struct?
argsA {
pub a: i32
}
argsB {
pub b: i32
}
fn foo(args : argsB) { ... }
fo foo(args : argsA) { ... }
Then you need to dispatch by argument type. You also need to declare structs for every permutation of types you might want to allow. Whereas using keyword arguments:
fn foo(b => arg : int32) { ... }
fn foo(a => arg : int32) { ... }
This means you can overload WITHOUT wrapping structs or having different types. I’ve already shown how this is useful by having both slice(to => 10)
and slice(from => 5)
that both take a usize
but do different things. You can think of this proposal as syntactic sugar because nobody is going to write ACTUAL code with everything wrapped in a struct (it’s not as convenient as in JS).
The drawbacks of default arguments is that they have to have a default value! What if I don’t want a default value?
unwrap(self, or => default: T) -> T
unwrap(self) -> T
let foo : Option<i32> = None;
if I call with just foo.unwrap()
it should panic!
, but if I call with foo.unwrap(or => 0)
it should default to 0
This might be a bad example (let’s say you want to have unwrap
be easy to find in the codebase so you can see what can panic!
), but the point remains the same: sometimes you want to do something else when an argument is missing without any runtime checks.