Discussion: optional parameters after params #1458
Replies: 9 comments 2 replies
-
How would the compiler disambiguate between an argument that belongs in the params array vs. those trailing optional arguments? I guess one method would be to require that the following optional parameters be called as named arguments: M(1, 2, 3);
// interpreted as
M(args: new object[] { 1, 2, 3 }, line: -1);
M(1, 2, line: 3);
// interpreted as
M(args: new object[] { 1, 2 }, line: 3); |
Beta Was this translation helpful? Give feedback.
-
@HaloFour Hasn't an LDT member discussed allowing named parameters after params in this repo recently? *Edit: sorry, meant specifically after the |
Beta Was this translation helpful? Give feedback.
-
You mean allowing unnamed params after named params? That shipped with C# 7.2, but the compiler resolves the trailing unnamed arguments by position. That doesn't really work well with |
Beta Was this translation helpful? Give feedback.
-
No, I'm pretty sure that's not what I'm thinking of. I use that feature all the time. |
Beta Was this translation helpful? Give feedback.
-
in expanded form, overload resolution adds an "effective parameter" per each argument in argument list, so naturally the algorithm never reaches the optional parameters after params. that's why leading optional parameters would never match a positional argument. after we find the best candidate, all missing optional arguments will default to their default value (or caller info). if you do want to pass a value to leading optional parameters, you'll have to use named arguments for both params and leading optionals. |
Beta Was this translation helpful? Give feedback.
-
Oh sorry, read that wrong. You mean named parameters after a void M(int a, params int[] b, int c) { }
M(1, 2, 3, c: 4);
// interpreted as
M(a: 1, b: new int[] { 2, 3 }, c: 4); I think that makes sense. But if void M(int a, params int b, int c = -1) { }
M(1, 2, 3, 4);
// interpreted as
M(a: 1, b: new int[] { 2, 3, 4 }, c: -1);
M(1, 2, 3, c: 4);
// interpreted as
M(a: 1, b: new int[] { 2, 3 }, c: 4); I think that would work pretty well, honestly. |
Beta Was this translation helpful? Give feedback.
-
haven't thought of that. I think it could work, perhaps it makes sense to have non-optionals after params? I don't know how that could be useful though, but the above example can work for passing leading optionals without changing params args to array creation with a named argument. |
Beta Was this translation helpful? Give feedback.
-
I think pattern-based ICollection and Tuple would solve this I mean in my opinion I prefer to write public void MultiParams(int i,IEnumerable<string> ss,IEnumerable<float> fs,bool option = false)
{
}
var vector3 = (0f,0f,0f);
MultiParams(0,("a","b","c"),vector3); // Tuple of same type will became ICollection<type> by compiler |
Beta Was this translation helpful? Give feedback.
-
Another possible use case is when there is an optional |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
The main use case is to use caller info attributes with params.
leading optional parameters would never match a positional argument.
Beta Was this translation helpful? Give feedback.
All reactions