-
Notifications
You must be signed in to change notification settings - Fork 21
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
Support optional arguments for optional parameters #1167
Comments
Regarding the
I think it makes sense to have it there since it's a default representation for the optional parameters in F#, and it should already be familiar for anyone who knows F#. |
Would it be possible to change Approach 2 into the proposed approach? I find the proposed approach the most elegant of the 3 and the most C# familiar (but maybe it's just my bias). |
Would be good to align this suggestion with #1136 ? . I like the suggested approach in your suggestion :) |
@jasiozet We definitely can't make this the only approach, because this and C#-style are less powerful than F#-style. Here, you can't know whether the caller passed the default value or didn't pass any value. In F#-style, the case where the caller didn't pass any value uses I would be fine with making this a syntax sugar for C#-style though. |
From my perspective this would still be an improvement. Thank you for providing the context #:+1: |
Good point.
Good point. F# compiler can do that for us behind the scene and make it compatible with the general .NET approach too. |
Regarding this point:
We would not want to deprecate the existing use of The public class C {
// Compiles
public void MAtttributes([Optional, DefaultParameterValue(1)] object i) { }
// error CS1763: 'i' is of type 'object'. A default parameter value of a reference type other than string can only be initialized with null
public void MSyntax(object i = 1) { }
} And so if you need the ability to declare that I also think that should this be pursued, then the following issues should also be considered in scope: https://github.com/dotnet/fsharp/issues?q=is%3Aissue+is%3Aopen+sort%3Aupdated-desc+DefaultParameterValue |
I'm labelling this as approved - I don't see any reason not to also support these given we interop with them anyway, and there are legitimate reasons to need them in interop |
Perhaps, it would be nice to have described approach with |
I'm also for two improvements in one. Compile time optional argument like in C# via |
I propose we simplify setting optional arguments for optional method parameters by allowing them to be set when the parameter is defined (like C# Optional Arguments).
Existing approaches:
Existing Approach 1
Existing Approach 2 (for C# interop)
Desired Approach
I would also expect (if possible) to be able to leave out the optional parameter's data type since it can be inferred from the default argument:
Pros and Cons
The advantages of making this adjustment to F# are:
The disadvantages of making this adjustment to F# are:
Extra information
Estimated cost (XS, S, M, L, XL, XXL):
I would guess this to be a S to M cost.
NOTE 1
In the Desired Approaches above, I did not include a
?
before thediscount
parameter; however, require it may ease implementation. Another benefit in requiring?
would be to maintain consistency with the established syntax for declaring an optional parameter.NOTE 2
I learned while making this proposal that decimal values cannot be used as literals.
In C#, trying to set a decimal default argument will result in the following error:
"CS1750: A value of type 'double' cannot be used as a default parameter because there are no standard conversions to type 'decimal'."
In the F# version above ("Existing Approach 2 (for C# interop)"), this will also fail with the following error:
"This is not a valid constant expression or custom attribute value."
I had to change the example in my proposal to use a double as the default value.
This does demonstrate a certain elegance to the native F# approach of using
defaultArg
which allows you to use an optional decimal and sidesteps this issue entirely.Related suggestions: (put links to related suggestions here)
The closest I could find to this issue posted here is this one by @baronfel:
Emit Optional;DefaultParameterValue by default for interop between F# and C#
Affidavit (please submit!)
Please tick this by placing a cross in the box:
Please tick all that apply:
For Readers
If you would like to see this issue implemented, please click the 👍 emoji on this issue. These counts are used to generally order the suggestions by engagement.
The text was updated successfully, but these errors were encountered: