Named arguments increase readability a lot

Stupid thing that just came to my mind and that works today (using unstable features):

Just implement FnOnce<()> for a struct!

Looks like this (or even nicer with a procedural macro):

named_args!{
    fn do_something( arg1: i32, arg2: i32 ) -> i32 {
        arg1 + arg2
    }
}

fn main() {
    // Both are valid: 
    println!("{}",
        do_something{ arg1: 18, arg2: 24 }()
    );
    println!("{}",
        do_something(18, 24)
    );
}

(playground)

my macro does somewhat support generics, for example:

named_args!{
    fn my_clone<'a, T>(source: &'a T) -> T
    where T: Clone
    {
        source.clone()
    }
}

fn foo() {
    let x = 1;
    let y = my_clone{source: &x}();
}
8 Likes