首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在F#中使用System.Collections队列类时出现类型参数错误

在F#中使用System.Collections队列类时出现类型参数错误
EN

Stack Overflow用户
提问于 2012-04-21 10:12:44
回答 1查看 400关注 0票数 2

在给定here的情况下,尝试运行阻塞队列示例。

使用该示例中所示的.NET Queue类,我得到以下错误:

代码语言:javascript
复制
let queue = new Queue<_>()

“非泛型类型System.Collection.Queue不需要任何类型参数”

如果从队列创建中删除了类型参数,则会更改type BlockingQueueAgent<'T>(maxLength)的泛型性质。

除了实现我自己的queue类之外,是否还有其他方法可以使用示例程序中使用的.NET Queue类?下面给出了完整的代码,以防更具说明性。

代码语言:javascript
复制
open System
open System.IO
open System.Collections
//open System.Runtime.Serialization.Formatters.Binary

///defining Agent
type Agent<'T> = MailboxProcessor<'T>

///defining Message
type internal BlockingAgentMessage<'T> = 
  | Get of AsyncReplyChannel<'T>
  | Add of 'T * AsyncReplyChannel<unit> 

/// Agent-based implementation of producer/consumer problem 
type BlockingQueueAgent<'T>(maxLength) =
    let agent = Agent.Start(fun agent ->
        let queue = new Queue<_>()
        //let queue = new Queue()
        // State machine running inside the agent
        let rec emptyQueue() = 
            agent.Scan(fun msg ->
                match msg with 
                | Add(value, reply) -> Some(enqueueAndContinue(value, reply))
                | _ -> None )
        and fullQueue() = 
            agent.Scan(fun msg ->
                match msg with 
                | Get(reply) -> Some(dequeueAndContinue(reply))
                | _ -> None )
        and runningQueue() = async {
            let! msg = agent.Receive() 
            match msg with 
            | Add(value, reply) -> return! enqueueAndContinue(value, reply)
            | Get(reply) -> return! dequeueAndContinue(reply) }
        and enqueueAndContinue (value, reply) = async {
            queue.Enqueue(value)
            reply.Reply() 
            return! chooseState() }
        and dequeueAndContinue (reply) = async { 
            reply.Reply(queue.Dequeue())
            return! chooseState() }
        and chooseState() = 
            if queue.Count = 0 then emptyQueue()
            elif queue.Count = maxLength then fullQueue()
            else runningQueue()

        // Start with an empty queue
        emptyQueue() )

    /// Asynchronously adds item to the queue. If the queue
    /// is full, it blocks until some items are removed.
    member x.AsyncAdd(v:'T) = 
        agent.PostAndAsyncReply(fun ch -> Add(v, ch))

    /// Asynchronously gets item from the queue. If the queue
    /// is empty, it blocks until some items are added.
    member x.AsyncGet() = 
        agent.PostAndAsyncReply(Get)




let ag = new BlockingQueueAgent<int>(3)

let writer() = async { 
    for i in 0 .. 10 do 
        do! ag.AsyncAdd(i)
        printfn "Added: %d" i }

let reader () = async { 
    while true do
        let! v = ag.AsyncGet()
        do! Async.Sleep(1000)
        printfn "Got: %d" v }

reader () |> Async.Start
writer () |> Async.Start
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-04-21 10:15:24

我想你要找的是System.Collections.Generic.Queue而不是System.Collections.Queue

变化

代码语言:javascript
复制
open System.Collections

代码语言:javascript
复制
open System.Collections.Generic
票数 9
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10255606

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档