Allow optional arguments in the middle of the argument list #2991
Replies: 8 comments
-
|
The main case where this would be interesting for me is having a combination of:
|
Beta Was this translation helpful? Give feedback.
-
|
Looks like both the compiler and the tooling support consuming assemblies where a method has optional parameters followed by required parameters: As you can see the compiler supports invoking those methods as long as the required parameters are explicitly specified, which requires using named arguments. |
Beta Was this translation helpful? Give feedback.
-
|
@HaloFour That's fascinating. @ekolis I'm curious: would your use case be completely covered if the language just allowed trailing required arguments? If we retained the requirement that optional parameters appear in sequence, with zero or more required parameters before and after, I think it would be easier to implement and use. |
Beta Was this translation helpful? Give feedback.
-
|
@sharwell Trailing required arguments? Isn't that semantically identical to what I proposed? Or is there some subtle difference I'm missing? edit: oh, you're saying all the required arguments have to come together, you can't do this: I would prefer there be no restrictions on the placement of arguments. |
Beta Was this translation helpful? Give feedback.
-
|
@ekolis could you give some real examples of APIs where the above would be desirable both from an API production and consumption perspective? Thanks! |
Beta Was this translation helpful? Give feedback.
-
|
You can already achieve this today using The C# compiler recognizes and correctly supports these attributes if manually specified (and emits them under the covers). |
Beta Was this translation helpful? Give feedback.
-
|
Interesting, so C# already supports consuming said assemblies and producing said assemblies through manually applying pseudo-attributes. Is there a reason C# doesn't permit optional parameter syntax in this situation? |
Beta Was this translation helpful? Give feedback.
-
|
@CyrusNajmabadi, there are a number of native interop APIs (such as in DirectX) where the optional parameters arent always trailing. APIs such as That being said, I expect it is rare enough that the manual workaround is sufficient (it's what I do for the interop scenario right now). |
Beta Was this translation helpful? Give feedback.

Uh oh!
There was an error while loading. Please reload this page.
-
For instance:
To call it with a default value for
yjust put two commas in a row:Or maybe to be clearer use a discard:
Beta Was this translation helpful? Give feedback.
All reactions