gpt4 book ai didi

generics - F# 通用问题 : could not be generalized because it would escape its scope

转载 作者:行者123 更新时间:2023-12-05 02:25:06 26 4
gpt4 key购买 nike

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

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

我正尝试这样使用它:

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


let findTickersMemoize = memoizeSingleParamWithCallback<ResultObject array, string> findTickers

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

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

我的两个问题是:

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

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

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

但是我希望能够内存,不仅仅是具有以下功能的函数:String Action<_> 签名,理想情况下,字符串可以是 int、float、object - 任何...

最佳答案

问题是您提供了一个类型参数 '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)

现在 F# 推断出最通用的适用类型,结果为 createCache正确取决于两个隐式类型参数。

关于generics - F# 通用问题 : could not be generalized because it would escape its scope,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4410591/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com