【发布时间】:2021-04-16 09:30:04
【问题描述】:
我可以做到(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>>'
那么,我在类型注释方面做错了吗?
【问题讨论】:
-
当我尝试将具有重载的方法分配给变量时,我收到建议“可能需要类型注释。” - 您是否尝试这样做?
-
@Christian 欢迎来到 F# 社区!
-
这确实适用于简单的情况,例如
let m: char -> int = "Hello".IndexOf;;,选择System.String.IndexOf(char)而不是System.String.IndexOf(string)。但是我想要的方法的签名(来自 octokit.net 库)更复杂,比如Task<IReadOnlyList<Issue>> GetAllForRepository(string owner, string name)。可能是因为Task<...>返回值。 -
你试过
m: ((string * string) -> System.Threading.Tasks.Task<System.Collections.Generic.IReadOnlyList<Octokit.Issue>>)吗?
标签: function f# overloading first-class