How to consume BlockingCollection<'a>.TryTake in F#

Deadly 提交于 2020-02-24 04:22:35

问题


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

Heres the signature:

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

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

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

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

Cheers!


回答1:


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.

For example:

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)


来源:https://stackoverflow.com/questions/4927649/how-to-consume-blockingcollectiona-trytake-in-f

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!