【问题标题】:How to consume BlockingCollection<'a>.TryTake in F#如何在 F# 中使用 BlockingCollection<'a>.TryTake
【发布时间】:2011-02-07 22:56:16
【问题描述】:

如何在 BlockingCollection 上使用 TryTake 方法以毫秒为单位传递超时时间?

这是签名:

BlockingCollection.TryTake(item: byref, millisecondsTimeout: int) : bool

是否可以使用 Tuple 方法来避免像 Dictionary.TryGet 方法那样传递 ref 类型?


让成功,item = myDictionary.TryGetValue(client)

我正在努力解决这个特殊的签名,任何建议都会很棒。

干杯!

【问题讨论】:

    标签: f# tuples task-parallel-library


    【解决方案1】:

    我相信您只能对出现在参数列表末尾byref参数使用该技术(这类似于可选参数的规则)。因此,如果 BlockingCollection.TryTake 使用签名 int * 'T byref -&gt; bool 定义它会起作用,但由于它被定义为 'T byref * int -&gt; bool 它不会。

    例如:

    open System.Runtime.InteropServices
    
    type T =
      static member Meth1(a:int, [<Out>]b:string byref, [<Out>]c:bool byref) : char = 
        b <- sprintf "%i" a
        c <- a % 2 = 0
        char a
      static member Meth2([<Out>]b:string byref, [<Out>]c:bool byref, a:int) : char = 
        b <- sprintf "%i" a
        c <- a % 2 = 0
        char a
    
    //  ok
    let (r,b,c) = T.Meth1(5)
    //  ok
    let (r,c) = T.Meth1(5,ref "test")
    // ok
    let r = T.Meth1(5, ref "test", ref true)
    // doesn't compile
    let (r,b,c) = T.Meth2(5)
    // doesn't compile
    let (r,c) = T.Meth2(ref "test", 5)
    // ok
    let r = T.Meth2(ref "test", ref true, 5)
    

    【讨论】:

    • 考虑到,如果我不得不对 F# 中的 BlockingCollection 做大量工作,我可能会创建一个 TryTake 的扩展方法重载,它接受一个整数并返回一个元组。
    • 我必须研究一下,因为我会经常使用它,设法让自己混淆创建传入的 ref T
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-17
    • 2014-09-07
    • 2021-12-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多