I can do this (F# FSI):
let o = Object()
let m = o.GetHashCode;; //[1]
val o : Object
val m : (unit -> int)
which makes method GetHashCode
accessible and callable as a first-class function in binding m
:
m ();;
val it : int = 12345678
How would I go about doing this if there existed an overload for the method in question, say an additional Object.GetHashCode(int whateverParameter)
? I now can only get the 'first' (shortest?) method. Can I provide extra parameter type information in call 1 to specify which overload I desire?
EDIT
Thanks for your input so far. Type annotations seem the way to go. Albeit, for my more complicated example I still cannot figure out how to correctly do that.
Here are the concrete method signatures from the type I am dealing with (it comes from the octokit.net library, so you need something like #r "nuget:include=Octokit"
in your fsi [with /langversion:preview
]).
I am interested in retrieving a specific overload of GetAllForRepository
, incidentally the first one listed: GetAllForRepository(string, string)
.
open Octokit
let client = GitHubClient(...)
client.Issue.GetType().GetMethods()
|> Seq.filter (fun methodInfo -> methodInfo.Name.StartsWith("GetAllForRepository"))
|> Seq.map (fun methodInfo -> (methodInfo.GetParameters(), methodInfo.ReturnType))
|> Seq.map (fun (parameterInfos, returnType) ->
(parameterInfos |> Seq.map (fun parameterInfo -> parameterInfo.ParameterType.FullName), returnType.FullName))
val it : seq<seq<string> * string> =
seq
[(seq ["System.String"; "System.String"],
"System.Threading.Tasks.Task`1[[System.Collections.Generic.IReadOnlyList`1[[Octokit.Issue, Octokit, Version=0.48.0.0, Culture=neutral, PublicKeyToken=null]], System.Private.CoreLib, Version=5.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]]");
(seq ["System.Int64"],
"System.Threading.Tasks.Task`1[[System.Collections.Generic.IReadOnlyList`1[[Octokit.Issue, Octokit, Version=0.48.0.0, Culture=neutral, PublicKeyToken=null]], System.Private.CoreLib, Version=5.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]]");
(seq ["System.String"; "System.String"; "Octokit.ApiOptions"],
"System.Threading.Tasks.Task`1[[System.Collections.Generic.IReadOnlyList`1[[Octokit.Issue, Octokit, Version=0.48.0.0, Culture=neutral, PublicKeyToken=null]], System.Private.CoreLib, Version=5.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]]");
(seq ["System.Int64"; "Octokit.ApiOptions"],
"System.Threading.Tasks.Task`1[[System.Collections.Generic.IReadOnlyList`1[[Octokit.Issue, Octokit, Version=0.48.0.0, Culture=neutral, PublicKeyToken=null]], System.Private.CoreLib, Version=5.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]]");
(seq ["System.String"; "System.String"; "Octokit.RepositoryIssueRequest"],
"System.Threading.Tasks.Task`1[[System.Collections.Generic.IReadOnlyList`1[[Octokit.Issue, Octokit, Version=0.48.0.0, Culture=neutral, PublicKeyToken=null]], System.Private.CoreLib, Version=5.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]]");
(seq ["System.Int64"; "Octokit.RepositoryIssueRequest"],
"System.Threading.Tasks.Task`1[[System.Collections.Generic.IReadOnlyList`1[[Octokit.Issue, Octokit, Version=0.48.0.0, Culture=neutral, PublicKeyToken=null]], System.Private.CoreLib, Version=5.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]]");
(seq
["System.String"; "System.String"; "Octokit.RepositoryIssueRequest";
"Octokit.ApiOptions"],
"System.Threading.Tasks.Task`1[[System.Collections.Generic.IReadOnlyList`1[[Octokit.Issue, Octokit, Version=0.48.0.0, Culture=neutral, PublicKeyToken=null]], System.Private.CoreLib, Version=5.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]]");
(seq
["System.Int64"; "Octokit.RepositoryIssueRequest";
"Octokit.ApiOptions"],
"System.Threading.Tasks.Task`1[[System.Collections.Generic.IReadOnlyList`1[[Octokit.Issue, Octokit, Version=0.48.0.0, Culture=neutral, PublicKeyToken=null]], System.Private.CoreLib, Version=5.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]]")]
Now, the 'shortest' signature takes one int64
parameter:
let m = client.Issue.GetAllForRepository;;
val m :
(int64 ->
System.Threading.Tasks.Task<System.Collections.Generic.IReadOnlyList<Octokit.Issue>>)
I can explicitly declare the signature and receive the same method:
let m: (int64 -> System.Threading.Tasks.Task<System.Collections.Generic.IReadOnlyList<Octokit.Issue>>) = client.Issue.GetAllForRepository;;
val m :
(int64 ->
System.Threading.Tasks.Task<System.Collections.Generic.IReadOnlyList<Octokit.Issue>>)
But accessing the (string, string)
version gives me:
let m: (string -> string -> System.Threading.Tasks.Task<System.Collections.Generic.IReadOnlyList<Octokit.Issue>>) = client.Issue.GetAllForRepository;;
let m: (string -> string -> System.Threading.Tasks.Task<System.Collections.Generic.IReadOnlyList<Octokit.Issue>>) = client.Issue.GetAllForRepository;;
--------------------------------------------------------------------------------------------------------------------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
stdin(33,117): error FS0193: Type constraint mismatch. The type
'string'
is not compatible with type
'int64'
Similar error occur when trying to access any other overload with more than one parameter:
let m: (int64 -> Octokit.RepositoryIssueRequest -> System.Threading.Tasks.Task<System.Collections.Generic.IReadOnlyList<Octokit.Issue>>) = client.Issue.GetAllForRepository;;
let m: (int64 -> Octokit.RepositoryIssueRequest -> System.Threading.Tasks.Task<System.Collections.Generic.IReadOnlyList<Octokit.Issue>>) = client.Issue.GetAllForRepository;;
-------------------------------------------------------------------------------------------------------------------------------------------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
stdin(37,140): error FS0001: This expression was expected to have type
'Octokit.RepositoryIssueRequest -> System.Threading.Tasks.Task<System.Collections.Generic.IReadOnlyList<Octokit.Issue>>'
but here has type
'System.Threading.Tasks.Task<System.Collections.Generic.IReadOnlyList<Octokit.Issue>>'
let m: (int64 -> Octokit.RepositoryIssueRequest -> Octokit.ApiOptions -> System.Threading.Tasks.Task<System.Collections.Generic.IReadOnlyList<Octokit.Issue>>) = client.Issue.- GetAllForRepository;;
let m: (int64 -> Octokit.RepositoryIssueRequest -> Octokit.ApiOptions -> System.Threading.Tasks.Task<System.Collections.Generic.IReadOnlyList<Octokit.Issue>>) = client.Issue.GetAllForRepository;;
-----------------------------------------------------------------------------------------------------------------------------------------------------------------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
stdin(36,162): error FS0001: This expression was expected to have type
'Octokit.RepositoryIssueRequest -> Octokit.ApiOptions -> System.Threading.Tasks.Task<System.Collections.Generic.IReadOnlyList<Octokit.Issue>>'
but here has type
'System.Threading.Tasks.Task<System.Collections.Generic.IReadOnlyList<Octokit.Issue>>'
So, am I doing something wrong with regard to the type annotations?
In response to your edit, the method's input is string * string
, not string -> string
(i.e. uncurried vs. curried), so I think this will work:
let m : (string * string -> _) = client.Issue.GetAllForRepository