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
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.
EDIT
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
Yet another solution, simply define a bunch of overloaded extension members on
Dictionary<'a,'b>
:Of course
values
here is not immutable like in your question, but I'm sure you could employ the same strategy in that goal.I had a similar problem in FsSql and I just tucked away boxing in a function:
A variation of Stephen's idea:
Here's another "solution" which is inspired from Brian's suggestion but it uses reflection so there is a time and safety cost.
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.Here's a solution, following kvb's suggestion (probably the most concise, and clearest, so far):