Search code examples
f#powerpack

How HashMultiMap mutate in Type instance


How add key-value pair to instance on HashMultiMap that is member of declared by the user type? maybe I'm doing something wrong

 #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

Output:

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

Solution

  • Every time you call this.tmp you create a new multimap - you want to use

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

    note that new is only called once