开发者

F# Generic Problem: could not be generalized because it would escape its scope

开发者 https://www.devze.com 2023-01-30 12:08 出处:网络
I have this defined in a common place: [<DataContract>] type ResultObject = { [<DataMember>]

I have this defined in a common place:

[<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 a开发者_JS百科n 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)

And am attempting to use it as so:

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


let findTickersMemoize = memoizeSingleParamWithCallback<ResultObject array, string>  findTickers 

and recieve this error on the memoize function definition:

This code is not sufficiently generic. The type variable 'P when 'P : equality could not be generalized because it would escape its scope.

My two questions are:

  1. What is this error telling me
  2. Is there a way to overcome this error

Everythign compiles by removing by typing parameter to string:

 fun (parameter: 'P) (callback: Action<_>) ->
     ()

HOWEVER I want to be able to memoize, more than functions with a: String Action<_> signature, ideally the string could be an int, float, object - whatever...


The issue is that you have provided a single type parameter 'T on your definition of createCache, but when you instantiate it in memoizeSingleParamWithCallback, you want to get back a Dictionary<'P, 'R option>. You can actually just remove some of the type parameters and annotations to get your code to work:

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)

Now F# infers the most general types that are applicable, which results in createCache correctly depending on two implicit type parameters.

0

精彩评论

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

关注公众号