如何在 F# 中使用 BlockingCollection<'a>.TryTake [英] How to consume BlockingCollection<'a>.TryTake in F#
问题描述
如何在 BlockingCollection<'a> 上使用 TryTake 方法以毫秒为单位传递超时时间?
How do I go about using the TryTake method on a BlockingCollection<'a> passing in a timeout period in milliseconds?
签名如下:
BlockingCollection.TryTake(item: byref, millisecondsTimeout: int) : 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)
我正在为这个特殊的签名而苦苦挣扎,任何建议都会很棒.
Im struggling with this particular signature, any suggestions would be great.
干杯!
推荐答案
我相信你只能对出现在 end 的 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;&#39;a&gt;.TryTake的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!