FWIW, I had been working on a proc-macro to allow named & default parameters to be used. I have a general approach in mind that should allow it to work with generics in bare functions, methods in impl blocks and trait methods. I haven’t gotten around to documenting though. And I haven’t worked on it for several months.
The basic approach though is to generate a builder struct at a location relative to the function, and create-able through a nice named function (for top level functions, it’s implemented as a method in a module with the same name as the function, for a impl block methods and trait methods, it’ll be implemented as another method, with the name based on the function name). Then it provides a macro that translates a named syntax into builder calls, and then unpacks the builder values into the positional function call. And this is doable without requiring the end user of the methods to import anything extra (relative to the normal positional syntax).
It should emFWIW, I had been working on a proc-macro to allow named & default parameters to be used. I have a general approach in mind that should allow it to work with generics in bare functions, methods in impl blocks and trait methods. I haven’t gotten around to documenting though. And I haven’t worked on it for several months.
The basic approach though is to generate a builder struct at a location relative to the function, and create-able through a nice named function (for top level functions, it’s implemented as a method in a module with the same name as the function, for a impl block methods and trait methods, it’ll be implemented as another method, with the name based on the function name). Then it provides a macro that translates a named syntax into builder calls, and then unpacks the builder values into the positional function call. And this is doable without requiring the end user of the methods to import anything extra (relative to the normal positional syntax).
It should be able to enable the following calling convention:
-
{} is used to surround named arguments
- The named arguments must come after the positional arguments
-
=> in the named argument area
- Something akin to struct sugar -
{a => a} can be replaced by {a,}
I’d be happy to outline the plan better if anybody wants to pick it up and get it more implemented - currently the macro only works for bare methods without generics.
https://crates.io/crates/rubber_duck
Heck, if anybody to make a macro of this, but wanted to take a different approach to how the final syntax looks, or different call semantics, I’m more than willing to talk about approaches of how to implement it with proc macros (proc macros aren’t really a strength of mine, but I have spent some time trying to figure out how to abuse them for named/called syntax in a way that doesn’t require nightly).