Write-only BlockingQueue?

When using programming Actors / CSP I often use BlockingQueues for commands, which goes wonderfully with Kotlin’s when construct.

More often than not I have input methods that look like this:

fun doSomething(cmd: MyCommand) {
    this.inQueue.put(cmd);
}

I could lose the method when exposing inQueue directly, but I only want to expose put.

Are there interface providing read-only/write-only access to Queues? Can I do this with generics?

No, there is no such write-only interface implemented by BlockingQueue.

You can indicate your intent by exposing in-projection BlockingQueue<in MyCommand>, however it won’t prevent consuming values from that queue.

If you only need to put values to the queue from the outside, you can expose just put method itself:

class ClassWithQueue {
    private val queue = BlockingQueue<MyCommand>()
    
    // either as a property
    public val enqueue = { cmd: MyCommand -> queue.put(cmd) } 
    // or as a method
    public fun enqueue(cmd: MyCommand) { queue.put(cmd) }
}

I solved it like this:

interface InQueue<E> {
    fun put(cmd: E);
}

class CommandQueue<E>(q: SynchronousQueue<E>): InQueue<E>, BlockingQueue<E> by q
1 Like
// either as a property
public val enqueue = { cmd: MyCommand -> queue.put(cmd) } 
// or as a method
public fun enqueue(cmd: MyCommand) { queue.put(cmd) }

I’ve never used a “function”-property like this. Is there any benefit compared to traditional methods?