Generics F#泛型问题:无法泛化,因为它将超出其范围

Generics F#泛型问题:无法泛化,因为它将超出其范围,generics,f#,Generics,F#,我在一个常见的地方对此进行了定义: [<DataContract>] type ResultObject = { [<DataMember>] mutable field1: string [<DataMember>] mutable field2: string [<DataMember>] mutable field3: int } let createCache<'T> () =

我在一个常见的地方对此进行了定义:

[<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)
[]
类型ResultObject={
[]
可变字段1:字符串
[]
可变字段2:字符串
[]
可变字段3:int
}
让createCache(HashIdentity.Structural)
让memoizeSingleParamWithCallback()
//返回一个包含两个参数的函数,一个是函数TOMEMOIZE的参数,另一个是回调
fun(参数:'P)(回调:操作)->
//在参数值中形成唯一的缓存键
让键=参数
//检查缓存是否包含密钥
将cache.ContainsKey(键)与
//如果是这样,则使用缓存值调用回调(需要转换为某些值)
|true->callback.Invoke(缓存。[键])
//如果不是,则调用RPC函数,存储该值并执行回调
|错误->
//创建internim回调以截获RPC函数结果,
//存储该值,并执行最终回调
让updateCache(结果:'R选项)=
将结果与
//没有返回结果-调用回调时不返回任何结果
|无->
缓存。[键]

cache。[key]问题在于,您在
createCache
的定义中提供了一个类型参数
'T
,但当您在
memoizeSingleParamWithCallback
中实例化它时,您希望返回一个
字典。实际上,您可以删除一些类型参数和注释以使代码正常工作:

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)
let createCache()=
字典(HashIdentity.Structural)
让memoizeSingleParamWithCallback函数TomeMoize=
let cache=createCache()
//返回一个包含两个参数的函数,一个是函数TOMEMOIZE的参数,另一个是回调
fun(参数:'P)(回调:操作)->
//在参数值中形成唯一的缓存键
让键=参数
//检查缓存是否包含密钥
将cache.ContainsKey(键)与
//如果是这样,则使用缓存值调用回调(需要转换为某些值)
|true->callback.Invoke(缓存。[键])
//如果不是,则调用RPC函数,存储该值并执行回调
|错误->
//创建internim回调以截获RPC函数结果,
//存储该值,并执行最终回调
让updateCache(结果:'R选项)=
将结果与
//没有返回结果-调用回调时不返回任何结果
|无->
缓存。[键]

cache。[key]请提供一个完整的示例-如果没有
createCache
ResultObject
commonProxyRpc
的定义,很难判断出哪里出了问题。Thks-我想我已经添加了足够的coed,使ti成为一个更完整的示例。如果我使用一个字符串而不是'P'作为参数变量的类型,这一切都是可行的,但函数应该能够更通用一些
 fun (parameter: 'P) (callback: Action<_>) ->
     ()
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)