F# 通用问题:无法泛化,因为它会超出其范围

aka*_*nom 2 generics f#

我在一个共同的地方定义了这个:

[<DataContract>]
type ResultObject = {
    [<DataMember>]
    mutable field1: string
    [<DataMember>]
    mutable field2: string
    [<DataMember>]
    mutable field3: int 
}

let createCache<'T> () =
    Dictionary<_, 'T option>(HashIdentity.Structural)  


let memoizeSingleParamWithCallback<'R, 'P when 'P : equality>  functionToMemoize =    

    let cache = createCache<'R>() 

    // return a function that takes two parameters a parameter to the functionToMemoize  and a callback 
    fun (parameter: 'P) (callback: Action<_>) ->
        // form a unique cache key the parameterValue
        let key = parameter

        // check to see if the cache contains they key
        match cache.ContainsKey(key) with
        // if so invoke the callback with the cache value (need to conver to Some)
        | true -> callback.Invoke(cache.[key])
        // if not, invoke the RPC function, store the value, and perform the callback
        | false ->
            // create an internim callback to intercept the RPC function results, 
            //     store the value, and perform the final callback
            let updateCache (results: 'R option) = 
                match results with
                // no results returned - invoke call back with None none
                | None -> 
                    cache.[key] <- None
                    callback.Invoke(None)
                // results returned - store them and invoke the call back 
                | Some result -> 
                    cache.[key] <- Some(result)
                    callback.Invoke(Some(result))
            functionToMemoize parameter  <| new Action<_>(updateCache)
Run Code Online (Sandbox Code Playgroud)

我试图这样使用它:

let findTickers (partialTicker : String) (callbackUI : Action<_>) =
    let lstOfResultObjects = [{field1=""; field2=""; field3=3}]
    callbackUI.Invoke(Some(lstOfResultObjects))


let findTickersMemoize = memoizeSingleParamWithCallback<ResultObject array, string>  findTickers 
Run Code Online (Sandbox Code Playgroud)

并在 memoize 函数定义中收到此错误:

此代码不够通用。类型变量 'P 当 'P : 相等时不能泛化,因为它会超出其范围。

我的两个问题是:

  1. 这个错误告诉我什么
  2. 有没有办法克服这个错误

Everythign 通过在字符串中键入参数来删除:

 fun (parameter: 'P) (callback: Action<_>) ->
     ()
Run Code Online (Sandbox Code Playgroud)

但是,我希望能够记忆,不仅仅是具有以下特征的函数:String Action<_> 签名,理想情况下,字符串可以是 int、float、object - 无论如何......

kvb*_*kvb 5

问题是您'T在 的定义中提供了一个类型参数createCache,但是当您在 中实例化它时memoizeSingleParamWithCallback,您想返回一个Dictionary<'P, 'R option>. 您实际上可以删除一些类型参数和注释来让您的代码工作:

let createCache() =
    Dictionary<_, _>(HashIdentity.Structural)  


let memoizeSingleParamWithCallback  functionToMemoize =    

    let cache = createCache() 

    // return a function that takes two parameters a parameter to the functionToMemoize  and a callback 
    fun (parameter: 'P) (callback: Action<_>) ->
        // form a unique cache key the parameterValue
        let key = parameter

        // check to see if the cache contains they key
        match cache.ContainsKey(key) with
        // if so invoke the callback with the cache value (need to conver to Some)
        | true -> callback.Invoke(cache.[key])
        // if not, invoke the RPC function, store the value, and perform the callback
        | false ->
            // create an internim callback to intercept the RPC function results, 
            //     store the value, and perform the final callback
            let updateCache (results: 'R option) = 
                match results with
                // no results returned - invoke call back with None none
                | None -> 
                    cache.[key] <- None
                    callback.Invoke(None)
                // results returned - store them and invoke the call back 
                | Some result -> 
                    cache.[key] <- Some(result)
                    callback.Invoke(Some(result))
            functionToMemoize parameter  <| new Action<_>(updateCache)
Run Code Online (Sandbox Code Playgroud)

现在,F# 推断适用的最通用类型,这会createCache根据两个隐式类型参数正确生成。