Я определил это в общем месте:
[<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:
Этот код недостаточно универсален.Переменная типа «P, когда» P: равенство не может быть обобщена, поскольку она выходит за пределы своей области.
Мои два вопроса:
- Что говорит мне эта ошибка
- Есть ли способ преодолеть эту ошибку
Everythign компилируется путем удаления путем ввода параметра в строку:
fun (parameter: 'P) (callback: Action<_>) ->
()
ОДНАКО Я хочу иметь возможность запомнить, больше, чем функции с подписью: String Action <_>, в идеале строка может быть int, float, object - что угодно ...