本文介绍了如何在F#中使用BlockingCollection<'a> .TryTake的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How do I go about using the TryTake method on a BlockingCollection<'a> passing in a timeout period in milliseconds?

在这里签名:

BlockingCollection.TryTake(项目:byref,毫秒超时:整数):bool

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

is it possible to use the Tuple method of avoiding passing a ref type like on the Dictionary.TryGet methods?


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

i.e.
let success, item = myDictionary.TryGetValue(client)

我正在为这个特殊的签名而苦苦挣扎,任何建议都会很棒.

Im struggling with this particular signature, any suggestions would be great.

干杯!

推荐答案

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

I believe that you can only use that technique for byref parameters which occur at the end of the parameter list (this is similar to the rule for optional parameters). So if BlockingCollection.TryTake were defined with signature int * 'T byref -> bool it would work, but since it's defined as 'T byref * int -> bool it won't.

例如:

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&lt;'a&gt; .TryTake的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 13:19