fsharp / fsharp/fslang-suggestions
Make C# methods with both optional and ParamArrays parameters easier to use from F#
- Dominant language
- No language data
- Stars
- 373
- Forks
- 21
- PR merge metrics
- No merged PRs in 30d
Description
# Make C# methods with both optional and ParamArrays parameters easier to use from F#
I propose we ...
use the default value of optional arguments that precede a `[]` argument. Consider these two C# methods:
```csharp
public static string Works(this string source, string target = "xyz")
{
return target;
}
public static string DoesNotWork(this string source, string target = "xyz", params string[] args)
{
return target;
}
```
(I don't think them being extension methods are relevant, that's just the example use case I had on hand.)
The first method can be called from F# like this: `"test".Works()`. The latter method cannot be called from F# without supplying the optional `target` argument: `"test".DoesNotWork()`. Instead we get the following compilation error:
`FS0501 The member or object constructor 'DoesNotWork' takes 2 argument(s) but is here given 0. The required signature is '(extension) String.DoesNotWork(?target: string, [] args: string []) : string'.`
The existing way of approaching this problem in F# is ...
Finding the method definition and using its default value. In my example: `"test".DoesNotWork("xyz")`. This is however complicated by the fact that intellisense does not show the default value of optional parameters in F#, and the fact that Peek Definition/Go To Definition does not seem to work with methods defined in nuget packages. I get the message "Cannot navigate to symbol under the caret." when I try. I had to create a C# project, include the same nuget package, and use Peek Definition from there, which does work.
## Pros and Cons
The advantages of making this adjustment to F# are ...
Easier interop with C# and consistent behavior between methods that contain only optional parameters, and methods that contain both optional parameters and `params ...` parameters.
The disadvantages of making this adjustment to F# are ...
I cannot think of any.
## Extra information
Estimated cost (XS, S, M, L, XL, XXL): Not familiar enough with the codebase to estimate accurately, but I would guess small-ish.
Related suggestions: None that I could find.
## Affidavit (please submit!)
Please tick this by placing a cross in the box:
* [x] This is not a question (e.g. like one you might ask on [stackoverflow](http://stackoverflow.com)) and I have searched stackoverflow for discussions of this issue
* [x] I have [searched both open and closed suggestions on this site](http://github.com/fsharp/fslang-suggestions/issues) and believe this is not a duplicate
* [x] This is not something which has obviously "already been decided" in previous versions of F#. If you're questioning a fundamental design decision that has obviously already been taken (e.g. "Make F# untyped") then please don't submit it.
Please tick all that apply:
* [x] This is not a breaking change to the F# language design
* [x] I or my company would be willing to help implement and/or test this
## For Readers
If you would like to see this issue implemented, please click the :+1: emoji on this issue. These counts are used to generally order the suggestions by engagement.
Contributor guide
No contributing guide indexed for this repository
Research direction
Reproduce the two C# method examples and compare the F# calls with and without the optional target argument. Trace how F# handles optional and ParamArray parameters, then define compiler tests showing that the default target is used and that existing calls remain valid.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp, fsharp
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100