给定一个受成员约束约束的值,我可以将成员作为一等函数访问吗?

发布于 2024-11-08 02:23:38 字数 595 浏览 0 评论 0原文

是否可以将受约束的成员作为一等函数(给定一个对象)进行访问?如果是这样,正确的语法是什么?

  // Example: property getter as a first-class function
  type Test() =
     member x.Value = "42"

  let t = Test()
  let getter = t.get_Value // works as expected


  // now generically:
  let inline getGetter< ^a when ^a : (member get_Value : unit -> string)> item =
    // call getter
    let value = (^a : (member get_Value : unit -> string) item)
    // try to get getter as first-class function
    let getter = item.get_Value // doesn't compile: "Lookup on object of indeterminate type..."
    ()

Is it possible to access a constrained member as a first-class function (given an object)? If so, what is the correct syntax to use?

  // Example: property getter as a first-class function
  type Test() =
     member x.Value = "42"

  let t = Test()
  let getter = t.get_Value // works as expected


  // now generically:
  let inline getGetter< ^a when ^a : (member get_Value : unit -> string)> item =
    // call getter
    let value = (^a : (member get_Value : unit -> string) item)
    // try to get getter as first-class function
    let getter = item.get_Value // doesn't compile: "Lookup on object of indeterminate type..."
    ()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

坚持沉默 2024-11-15 02:23:38

我认为这就是您正在寻找的:

  type Test() =
     member x.Value = "42"

  let inline getGetter< ^a when ^a : (member get_Value : unit -> string)> item =
    fun () -> (^a : (member get_Value : unit -> string) item)

  let t = Test()
  let getter = getGetter t
  let value = getter()

I think this is what you are looking for:

  type Test() =
     member x.Value = "42"

  let inline getGetter< ^a when ^a : (member get_Value : unit -> string)> item =
    fun () -> (^a : (member get_Value : unit -> string) item)

  let t = Test()
  let getter = getGetter t
  let value = getter()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文