Go: "embedded type cannot be a type parameter"

52 Views Asked by At

I was writing a type safe wrapper over "request-response"-style goroutines using Go generics and stumbled upon a compile error:

type WorkerContext[In any, Out any] interface {
    Process(In) (Out, bool)
    Destroy()
}

type Worker[In any, Out any] struct {
    context WorkerContext[In, Out]
    in      chan In
    out     chan struct{Out; bool}
}

Here, a goroutine is represented by a Worker instance, and the user code that is executed within the goroutine to handle requests is represented by a WorkerContext interface (which may potentially hold arbitrary external resources, such as a child process that the goroutine is communicating with).

The idea here is that the WorkerContext.Process() function returns the result of processing a request, and a flag indicating if the external resource can accept more requests. Both values are transmitted via the channel back to the requesting thread. There, the flag is used to decide whether to put the worker (with its associated goroutine and the context) back into the worker pool or destroy it (creating a new one when the next request comes up).

However, I can't seem to declare such a channel:

chan struct{Out; bool}

It gives this error:

src/taskpool/taskpool.go:24:22: embedded field type cannot be a (pointer to a) type parameter

Why is that so? The restriction feels kinda arbitrary, and what is the idiomatic way to do what I want to do here?

0

There are 0 best solutions below