hsutter / cppfront

A personal experimental C++ Syntax 2 -> Syntax 1 compiler
Other
5.46k stars 238 forks source link

[SUGGESTION] Named function arguments #913

Open RPeschke opened 9 months ago

RPeschke commented 9 months ago

Hi,

I am not sure if someone has mention it already but i think named function arguments might be a helpful extension.

The implementation should basically be the same as in python.


void my_funtion(int p1, int p2) ;

int main() {
  my_funtion(p2=123, p1= 321);

}

I think most of us are already very familiar with the feature and the benefits are clear.

I see a special use case when it comes to simple data structs. For example I am in the process of writing a little ntuple library. This library can generate ntuple with some template magic.

example:


  auto my_tuple = nt::ntuple{
      subdetector = i ,
      sector{123},
      sector1 = std::string("asd"),
      axis = "4655",
      plane() = 1231,
     ax_maker(ex1) = 789  // ax_maker is a macro that generates a new axis on name "ex1" in-place 
    };
  assert(my_tuple.sector==  123);
  assert(my_tuple.sector1 ==  "asd");

However this only works if the term subdetector (sector/sector1/axis/plane ) is already defined previously.

I believe that for clarity and expressing intent, named function arguments are an essential feature that any language succeeding C++ must incorporate. While I recognize the complexity of lifetime considerations, I feel that the potential benefits make it a worthwhile endeavor.

PS: Thanks for the Great talk(CPPCON) and this wonderful project.

DISCLAIMERS TO SET EXPECTATIONS: I'm generally against language feature requests/changes unless they can be shown to improve simplicity, safety, or toolability in a quantifiable way. So:

Will your feature suggestion eliminate X% of security vulnerabilities of a given kind in current C++ code? If yes, please be specific about the classes of bugs that would go away, with an example or two (especially a link to a real CVE or two).

Will your feature suggestion automate or eliminate X% of current C++ guidance literature? If yes, please be specific about what current good guidance this helps make the default, and/or what guidelines we would no longer need to teach/learn or that would be simplified and how, with an example or two (especially a link to a real "Effective C++" or "C++ Core Guidelines" guideline or two). For ideas, you can refer to my CppCon 2020 talk starting at 10:31 where I summarize a categorized breakdown based on over 600 C++ guidance literature rules I cataloged and analyzed.

Describe alternatives you've considered. There's nearly always more than one way to improve something. What other options did you consider? Why is the one you're suggesting better than those?

JohelEGP commented 9 months ago

See https://github.com/hsutter/cppfront/issues/202#issuecomment-1375014503.

RPeschke commented 9 months ago

thanks.