Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type argument error using System.Collections Queue class in F#

Trying to run the Blocking Queue example given here.

Using the .NET Queue class as shown in that example, I'm getting the following error:

let queue = new Queue<_>()

"The non-generic type System.Collection.Queue does not expect any type arguments"

If the type argument is removed from the Queue creation, then the generic nature is changed for the type BlockingQueueAgent<'T>(maxLength).

Other than implementing my own queue class, is there any way to use the .NET Queue class as used in the sample program? The entire code is given below, in case that's more illustrative.

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
like image 908
AruniRC Avatar asked Apr 13 '26 06:04

AruniRC


1 Answers

I think you're looking for System.Collections.Generic.Queue instead of System.Collections.Queue.

Change

open System.Collections

to

open System.Collections.Generic
like image 84
Wesley Wiser Avatar answered Apr 17 '26 16:04

Wesley Wiser



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!