Pre RFC: Optionals arguments in rust

Feature Name: optional_function_arguments
Start Date: 2019-02-05
RFC PR:
Rust Issue:

Introduction

This RFC adds the possibility of using optional argument.

Motivation

In most languages today there is an optional argument system.
Indeed, in rust it already exists, but it is not really used by his complexity.
This feature can be really good for rust by simplifying rust programming and existing crates.

Detailed design

// This system use the already existing Option statement
// Because the usage of Option statement all already libraries works without any problem without any updates.
fn example_with_optional(i: i32,c: Option<i32>) {
	// Body of the function
}

// But here we can simply do that instead of writing Some of None
fn main() {
	example_with_optional(12);
	example_with_optional(12,13);
	// This can be compiled to:
	// example_with_optional(12,None);
	// example_with_optional(12,Some(13));
}

Alternatives

The alternatives is to use a more complete system like in Java, C++ or other languages, but this alternative require a Rebuild of rust functions system:

// This is more radical
fn example_with_optional(r: i32) {
	// Body of the function
}

// Here the addition of a new argument
fn example_with_optional(r: i32,e: i32) {
	// Body of the function
}

// And if we use that we can use different types
fn example_with_optional(r: i32,e: f32) {
	// Body of the function
}

fn main() {
	example_with_optional(1,1.4);
	example_with_optional(1);
	example_with_optional(1,78);
	// This code will work without the duplicated name error
}

// A method of implementation:
// This code can be compiled like that:

// This is more radical
fn example_with_optional__1(r: i32) {
	// Body of the function
}

// Here the addition of a new argument
fn example_with_optional__2(r: i32,e: i32) {
	// Body of the function
}

// And if we use that we can use different types
fn example_with_optional__3(r: i32,e: f32) {
	// Body of the function
}

fn main() {
	example_with_optional__3(1,1.4);
	example_with_optional__1(1);
	example_with_optional__2(1,78);
}

Conclusion

This can be a great feature for all rust programmers.
Indeed, this feature makes rust more ergonomic, simple and gives to beginners marks from other languages

I don't know if this RFC can be accepted.
Thank's for reading.
ccgauche.

Hiya! There is this Wishlist: functions with keyword args, optional args, and/or variable-arity argument (varargs) lists, which discusses a lot of variations of this, though it is labelled postponed right now.

By the way, pre-RFCs are more geared for the internals forum I think

1 Like

Here's my opinion:
The rust system works for explicitness, and this would perhaps not work with it. If I want to have two constructors for example:

fn create(_: usize) -> Self;
fn create(_: usize, _: f32) -> Self;

Then they should be named differently

fn with_num_blocks(blocks: usize) -> Self;
fn with_blocks_depths(blocks: usize, depth: f32) -> Self;

or better yet, use an Option

fn with_config(blocks: usize, depth: Option<f32>) -> Self;

and if there was an option to call the function with only a usize, or a usize, and an f32 then I'd loose the explicitness of there being the option to include the depth parameter. I'm actually finding it hard to explain my argument. I just feel like this is too implicit and wouldn't fit with rust and its explicitness with this kind of thing. I wouldn't be opposed to it; it's an idea which would probably be very useful, I just think it wouldn't fit well with rust.

I think it is not a problem because you can continue to do that but if you want you can use the new method.