给定以下代码:
static member private getIntValue (_map:Map<string, int>) (_key:string) =
if (_map.ContainsKey _key) then
_map.[_key], _map
else
let i = doSomething ()
i, if i > 0 then _map.Add (_key, i) else _map
static member private getDataFn<'T> (_getFn:Map<string, 'T> -> string -> 'T * Map<string, 'T>) =
let dataMap = ref Map.empty
fun _key ->
let value, updatedMap = _getFn !dataMap _key
dataMap := updatedMap
value
static member getIndexNumber = getDataFn<int> getIntValue
... getDataFn<'T>的函数定义(即fun _key -> ...)的第一行中dataMap引用单元格的值始终为空(即dataMap.Count = 0),无论有多少有时我调用 getIndexNumber。
显然,我希望只要将不存在的键传递给 getIndexNumber,就会更新 dataMap,但这并没有发生。每次都将一个空 Map 传递给 getIntValue。
为什么会这样?
(PS 我知道我可以只使用字典,这不是重点。)