function - F#:如何作为第一类对象访问 Dotnet 对象的方法的特定重载

标签 function f# overloading first-class

我可以做到这一点(F# FSI):

let o = Object()
let m = o.GetHashCode;; //[1]
val o : Object
val m : (unit -> int)

这使得方法GetHashCode可以作为绑定(bind)m中的一等函数进行访问和调用:

m ();;
val it : int = 12345678

如果相关方法存在重载(例如额外的 Object.GetHashCode(intwhateverParameter)),我将如何执行此操作?我现在只能得到“第一个”(最短?)方法。我可以在调用 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>>'

那么,我在类型注释方面做错了什么吗?

最佳答案

是的,类型注释将允许您选择所需的方法覆盖:

type MyClass() =
    member __.MyMethod() = 1
    member __.MyMethod(x : int) = 2

let o = MyClass()
let m1 : (unit -> int) = o.MyMethod
let m2 : (int -> int) = o.MyMethod
printfn "%A" <| m1 ()
printfn "%A" <| m2 0

关于function - F#:如何作为第一类对象访问 Dotnet 对象的方法的特定重载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65666862/

相关文章:

PHP 所有函数都应该先检查它们的参数类型吗?

list - F#:[0; 有什么区别? 1; 2; 3; 4; 5] 和 [0, 1, 2, 3, 4, 5]?

angular - 函数实现丢失或没有紧跟在声明之后,TypeScript 类

python - 从 Python 中的字符串列表处理函数

F#创建表达式的自定义属性

c# - F#:一个复杂的记录类型和一个 DataGridView

c++ - 赋值和添加运算符重载(2 个表和 int 的连接)

java - 在 Java 中查找方法重载计数

function - 为什么 Golang 允许两个函数在具有不同的接收者类型时具有相同的名称,但在它们具有不同的参数类型时则不允许?

c++ - 调用错误没有匹配函数