fslang-suggestions
fslang-suggestions copied to clipboard
Allow specifying subset of type arguments explicitly
Submitted by Tomas Petricek on 5/28/2014 12:00:00 AM
7 votes on UserVoice prior to migration
Say we have a generic type Frame<TRow, TColumn>. When using instance methods of the frame, it is possible to write a generic method that takes a single additional type parameter - for example, to get a column as a specific type:
frame.GetColumn<float>("Value")
However, doing the same thing using module and function is not possible, because the corresponding getCol function requires three type arguments (TRow, TCol and the additional one):
frame |> Frame.getCol<_, _, float> "Value"
It would be nice if F# had some mechanism that would allow specifying only subset of the type parameters. For example:
let getCol<[<RequiresExplicitTypeArguments>] 'T, 'TCol, 'TRow> frame = (...)
And then I could write just:
frame |> Frame.getCol<float> "Value"
Interestingly, this is also problem for extension methods. When you define a C#-style extension method for a generic type like frame, it also has three type arguments and it is impossible to call it with just a single type argument.
Alternatively, allow for auto-inferrable generic parameters expressed like this
Foo<'a, [<Inferred>] 'b, [<Inferred>] 'c when 'a :> Bar<'b,'c>>(v:'a) = class end
Then only Foo<int> is required at each use site and the other generic parameters are optional. Foo<int> would always be shorthand for Foo<int, _, _>
In certain situations where wildcards are not allowed, solving the type parameters must be possible "immediately". Exactly what that means technically would be TBD.
Original UserVoice Submission Archived Uservoice Comments
** by fslang-admin on 8/3/2015 12:00:00 AM **
Updating to planned to indicate this is approved in general terms. An RFC is needed.
Don Syme, F# Language and Core Library Evolution
This is a bit different to #629 but I'm merging them, because they are both really about inferring some generic parameters from others
FWIW, #629 had some relevant discussions and also a dozen or so upvotes, apparently that issue was slightly more discoverable or understandable to users. Good to see both merged though, they’re closely related.