简洁地创建IDictionary< _,obj>

问题描述:

是否有一种较短的方法来创建 IDictionary< _,obj> ,可能不需要装箱每个值?这就是我所拥有的.

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

let values =
  [ "a", box 1
    "b", box "foo"
    "c", box true ]
  |> dict

Dictionary< _,obj> .Add 可以不用装箱而调用,但是我想不出一种比我更短的使用方法.

Dictionary<_,obj>.Add can be called without boxing, but I couldn't figure out a way to use it that's shorter than what I have.

除了定义拳击员之外,我还希望有其他东西.

I'm hoping for something other than defining a boxing operator.

根据布赖恩(Brian)的建议,这是一种实现方法,但是它有其自身的问题.

Based on Brian's suggestion, here's one way to do it, but it has its own problems.

let values =
  Seq.zip ["a"; "b"; "c"] ([1; "foo"; true] : obj list) |> dict

以下是kvb的建议(到目前为止,可能是最简洁,最清晰的):

Here's a solution, following kvb's suggestion (probably the most concise, and clearest, so far):

let inline (=>) a b = a, box b

let values =
  [ "a" => 1
    "b" => "foo"
    "c" => true ]
  |> dict