开发者

Given a value constrained with a member constraint, can I access the member as a first-class function?

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开发者_如何学JAVA 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..."
    ()


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()
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜