给定受成员约束约束的值,我是否可以将该成员作为一等函数访问

  • 本文关键字:成员 约束 访问 函数 是否 f#
  • 更新时间 :
  • 英文 :


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

  // 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..."
    ()

我认为这就是您要查找的:

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

最新更新