我可以这样做(F# FSI):
let o = Object()
let m = o.GetHashCode;; //[1]
val o : Object
val m : (unit -> int)
这使得方法GetHashCode
可以作为绑定中的一流函数访问和调用m
:
m ();;
val it : int = 12345678
如果有问题的方法存在重载,我将如何执行此操作,比如说另外一个Object.GetHashCode(int whateverParameter)
?我现在只能得到“第一个”(最短的?)方法。我可以在调用1中提供额外的参数类型信息来指定我想要的重载吗?
编辑
感谢您迄今为止的意见。类型注释似乎是要走的路。尽管如此,对于我更复杂的例子,我仍然无法弄清楚如何正确地做到这一点。
以下是我正在处理的类型的具体方法签名(它来自octokit.net库,因此您需要在 fsi #r "nuget:include=Octokit"
[with /langversion:preview
] 中使用类似的东西)。
我有兴趣检索 的特定重载GetAllForRepository
,顺便说一下列出的第一个: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]]")]
现在,“最短”签名采用一个int64
参数:
let m = client.Issue.GetAllForRepository;;
val m :
(int64 ->
System.Threading.Tasks.Task<System.Collections.Generic.IReadOnlyList<Octokit.Issue>>)
我可以显式声明签名并接收相同的方法:
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>>)
但是访问该(string, string)
版本给了我:
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'
尝试使用多个参数访问任何其他重载时会发生类似错误:
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>>'
那么,我在类型注释方面做错了吗?