2

如何将键值对添加到用户类型声明的 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
4

1 回答 1

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只调用一次

于 2012-03-12T09:08:40.450 回答