Concisely creating an IDictionary<_,obj>

后端 未结 7 1525
萌比男神i
萌比男神i 2020-12-03 23:09

Is there a shorter way of creating an IDictionary<_,obj>, possibly without boxing every value? This is what I have.

let values =
  [ \"a\"         


        
7条回答
  •  盖世英雄少女心
    2020-12-03 23:49

    Yet another solution, simply define a bunch of overloaded extension members on Dictionary<'a,'b>:

    open System.Collections.Generic
    type Dictionary<'a,'b> with
        member this.Add(x1,y1,x2,y2) =
            this.Add(x1,y1)
            this.Add(x2,y2)
        member this.Add(x1,y1,x2,y2,x3,y3) =
            this.Add(x1,y1,x2,y2)
            this.Add(x3,y3)
        member this.Add(x1,y1,x2,y2,x3,y3,x4,y4) =
            this.Add(x1,y1,x2,y2,x3,y3)
            this.Add(x4,y4)
        member this.Add(x1,y1,x2,y2,x3,y3,x4,y4,x5,y5) =
            this.Add(x1,y1,x2,y2,x3,y3,x4,y4)
            this.Add(x5,y5)
        member this.Add(x1,y1,x2,y2,x3,y3,x4,y4,x5,y5,x6,y6) =
            this.Add(x1,y1,x2,y2,x3,y3,x4,y4,x5,y5)
            this.Add(x6,y6)
        //etc.
    
    let values = 
        let d = Dictionary<_,obj>()
        d.Add("a", 1, 
              "b", "foo", 
              "c", true)
        d
    

    Of course values here is not immutable like in your question, but I'm sure you could employ the same strategy in that goal.

提交回复
热议问题