-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
named parameters at function call site #479
Comments
A couple of notes:
const Overflow = enum {
Checked,
Unchecked,
};
add(2, 3, Overflow.Checked);
|
Tricks to simulate named parameters in C++ make situation much worse (e.g. in Boost http://www.boost.org/doc/libs/1_33_1/libs/graph/doc/bgl_named_params.html). In language D people bend themselves backward trying tricks (one of several attempts https://forum.dlang.org/post/ni1tr0$1kln$1@digitalmars.com ) instead of doing the proper thing. Even using good old enums or structs is needless complication. I support this feature. Things to consider:
Using word |
A couple relevant pieces of information:
|
Function overloading based on number of parameters was also rejected? This one is without surprises. |
Correct. |
We talked a little about why function overloading was rejected, but I failed to find any "Issues" that talked about why optional parameters were rejected. Is there a discussion on that you could point me to, I'm curious what the arguments for/against were. Specifically on optional parameters with default values, not with function overloading. |
Sigils (what I understand under the term) may not be necessary. Function may look like:
That's still 4 character shorter. |
I think using Note that using words over symbols in general makes a language simpler to learn/remember because it's easier to remember a word like "named" than to remember a special character; though this is at the cost of typing more characters. If I gave you a list of semantics to learn like the following, would it be easier to remember the word version of the symbol version?
The disadvantage I can see is that it's a few more characters. In my opinion the extra characters are worth the benefits listed above, and adding a new special character for this one use case isn't justified. |
I think you are right. |
Just a few questions: What happens with named varargs parameters? What would the following mean? How would you call them? Below are my thoughts.
Syntax In the calls, I have prefixed the name with a . to make it clear it's not a regular assignment - I think it is analogous to initializing a struct with a positional vs parametric initializer list.
Though I'm usually a strong proponent/supporter of sigils, in this case I agree that the named keyword is better. |
I think there is not a documented discussion. Feel free to start one. |
I like the idea of optionally naming parameters at call sites to disambiguate, with the condition that:
Point (1) is to prevent confusion and remove ambiguity about overloading and to make it clear that this feature is not meant to allow overloading or jumbling up of parameter order. Point (2) has potential to be problematic since it makes the parameter names a part of the function's interface, in that if a library provider changes a parameter name, it would affect all users of the library. It's how Swift does it, except in Swift naming the parameters is mandatory, not optional. So, using enums keeps things simpler IMO and does not require any new features. Although, another neat feature from Swift is implicit enum namespace when passing parameters.
But that can also be very confusing to read. Overall it's kind of a "nice to have" but I don't see it as essential in any way, and can probably be left for potentially future higher level languages that want to compile down to zig. |
If you introduce named parameters like this, haven't you already violated "one way to do things"? You now have two ways of taking function parameters, and it's not always obvious which one you should use. Here's a half-serious proposal: any function taking more than 2 non-enum parameters must be called with all named parameters, and otherwise they must be called without names. Then there would always be one way to do things, and when you have a function with lots of parameters, you know you'll be able to tell which one is which when reading code without an IDE.
I think this should be a feature. I don't see how it can be all that confusing. I think using enums and types to explicitly annotate function parameters is a very good practice, and anything that encourages this is positive. |
Solved with #485 (comment) |
For some functions it makes sense to include the name of one or more parameters at the call site. This is common if it's a flag type parameter, if the parameter is a "unit type" like "seconds"/"meters", or if the function takes multiple parameters of the same type whose order is important but not clear. It is proposed that a function definition can annotate certain parameters to be
named
in which case the caller is REQUIRED to write the name of those parameters at the call site. With this annotation the name at the call site should be REQUIRED instead of optional to maintain the "one way to do things" philosophy.The text was updated successfully, but these errors were encountered: