Concisely creating an IDictionary<_,obj>

后端 未结 7 1509
萌比男神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-04 00:12

    Here's the slickest thing I was able to whip up. It has more characters than your boxing version, but possibly feels a little less dirty. Note that the ^ is right-associative (it's the string concat operator inherited from ocaml), which lets it work like ::, and it has stronger precedence than ,, which is why the parenthesis are needed around the tuples.

    let inline (^+) (x1:'a,x2:'b) (xl:('a*obj) list) = 
        (x1,box x2)::xl
    
    let values =
      ("a", 1) ^+  ("b", "foo") ^+ ("c", true) ^+ []
      |> dict
    
    0 讨论(0)
提交回复
热议问题