HashMultiMap如何在Type实例中变异

时间:2012-03-12 09:04:13

标签: f# powerpack

如何在HashMultiMap上添加键值对,该HashMultiMap是用户类型声明的成员?也许我做错了什么

 #r"FSharp.PowerPack"
    type Test() = 
        member this.tmp = new HashMultiMap<string, int>(HashIdentity.Structural)
        member this.add name test = 
            this.tmp.Add(name, test)
    let t1 = new Test()
    t1.add "aaa" 1
    let a1 = t1.tmp.TryFind("aaa")
    let b1 = t1.tmp.Count
    //+
    let t2 = new HashMultiMap<string, int>(HashIdentity.Structural)
    t2.Add("aaa", 1)
    let a2 = t2.TryFind("aaa")
    let b2 = t2.Count

输出:

--> Referenced 'C:\Program Files\FSharpPowerPack-1.9.9.9\bin\FSharp.PowerPack.dll'


type Test =
  class
    new : unit -> Test
    member add : name:string -> test:int -> unit
    member tmp : HashMultiMap<string,int>
  end
val t1 : Test
val a1 : int option = None
val b1 : int = 0
val t2 : HashMultiMap<string,int>
val a2 : int option = Some 1
val b2 : int = 1

1 个答案:

答案 0 :(得分:3)

每次拨打this.tmp时都会创建一个新的多地图 - 您想要使用

type Test() = 
    let map =  new HashMultiMap<string, int>(HashIdentity.Structural)
    member this.tmp = map
    member this.add name test = 
        map.Add(name, test)

请注意,new仅被调用一次