-
Notifications
You must be signed in to change notification settings - Fork 251
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[SUGGESTION] Named arguments are somehow similar to designated initializers #330
Comments
I should mention that @hsutter in this commit already banned variable shadowing for typed scope functions. Therefore designated initializer doesn't conflict with constructor in the following code:
|
Val has.a feature like this which is cool. This would also help eliminate ambiguity of contructors. We just need to settle on rules of using this. vec1 : std::vector = (.val = 42, .size = 5); //(42, 42, 42, 42, 42)
vec2 : std::vector = (.arr = [42, 5]); //(42, 5);
This is very easy to distinguish. |
Yes, for Additionally with the help of parameter aliases (a variant of function aliases), we may change the name of parameters. This is useful when dealing with external APIs:
The syntax doesn't matter, I just wanted to share how function aliases can improve named arguments if C++2 supports them. |
Herb commented on named arguments here: #202 (comment) |
Thanks, that's what I've found:
I hope that means yes, the concept of named arguments and designated initializers will be unified 😁 |
Thanks! If I do pursue this path in the future I'll definitely have an eye on making it a general feature that works well across the space of named initialization cases. However, I'm going to close this as out of scope for now, as doing it right to work well with existing libraries would require at minimum fully parsing Cpp1 (and at least do name lookup and overload resolution) and even then wouldn't generally work the way you want. This line you mentioned above illustrates some of the problems:
Consider what it would take to try to support this code:
It's possible that this feature could be supported for Cpp2 code only, where we can avoid the third bullet's problem since there are no forward declarations. But even then it would require quite a bit of work for cppfront to do things it doesn't do yet today. So I'll keep this in kind, but probably only for Cpp2 code, and probably not until quite some time in the future. Sorry, but maybe someday! |
If for example this is the syntax (similar to C++1) for designated initializers in C++2:
Also the similar syntax can be used for named arguments in C++2:
Named arguments help to distinguish function call on overload resolution (e.g. this issue about
std::vector
initialization), and they are more readable than positional arguments.My suggestion is if someday C++2 supports named arguments, its syntax (it doesn't matter how it looks like) should be similar to the syntax of designated initializers.
Will your feature suggestion automate or eliminate X% of current C++ guidance literature?
Yes, less complex and uniform syntax will make C++2 simpler to learn and easier to remember.
The text was updated successfully, but these errors were encountered: