【问题标题】:Can I create a collection of arbitrary types in F#? [duplicate]我可以在 F# 中创建任意类型的集合吗? [复制]
【发布时间】:2014-12-03 15:44:12
【问题描述】:

很简单

let x = dict [(1, "Foo"); (2, 4)]

不起作用。所以我希望这会奏效。

let x:System.Collections.Generic.IDictionary<int,System.Object> = dict [(1, "foo"); (2, 4)]

也没有。我有点明白...

但是我该如何处理从外部库交给我的(任意类型的)集合?

let x = SomeCallToAExternalLibraryReturningArbitraryTypes()

【问题讨论】:

    标签: collections f#


    【解决方案1】:

    F# 是强类型的。您需要将对象转换为正确的类型。如果你想拥有 int * obj 的元组,那么:

    [(1, "Foo" :> obj); (2, 4 :> obj)] |> dict
    

    【讨论】:

    • box 在这里是惯用的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-13
    • 2019-01-05
    • 1970-01-01
    • 2011-08-18
    • 2020-12-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多