Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

redefine stream types #61

Closed
plastikfan opened this issue Aug 30, 2023 · 2 comments · Fixed by #62
Closed

redefine stream types #61

plastikfan opened this issue Aug 30, 2023 · 2 comments · Fixed by #62
Assignees
Labels
refactor Refactor code

Comments

@plastikfan
Copy link
Contributor

plastikfan commented Aug 30, 2023

The stream types have the Job embedded within them. Due to the design of go types, this gives us a problem that somethimes make working with the types difficult to understand.

If it were easy to translated between related types (specifically the channel types), we wouldn't have a problem.

Eg

If we have input and output types defined as:

type MyInput int
type MyOutput string
type MyInputJobStream boost.JobStream[MyInput]
type MyOutputJobStream boost.OutputStream[MyOutput]

we can define out work pool as:

boost.WorkerPool[MyInput, MyOutput ]

But the problem here is, if we try to use the predefined channel definitions, we find that incompatibles occur, as the type keyword introduces new incompatible types.

So, using OutputStream

type MyOutputJobStream boost.OutputStream[MyOutput]

.. is ok in isolation, but when we consider this:

func (p *WorkerPool[I, O]) Start(
	ctx context.Context,
	outputsChOut OutputStream[O],
) {
	p.run(ctx, p.private.workersJobsCh, outputsChOut)
}

this only works with:

	pool := boost.WorkerPool[MyInput, MyOutput]{}
	pool.Start(context.Background(),
		make(boost.OutputStream[MyOutput]),
	)

when in actual fact, we would rather use our custom channel type defs:

	pool.Start(context.Background(),
		make(MyOutputJobStream),
	)

which is not valid, due to incompatible types:

cannot use make(MyOutputJobStream) (value of type MyOutputJobStream) as boost.OutputStream[MyOutput] value in argument to pool.Startcompiler[InvalidChanAssign](https://pkg.go.dev/golang.org/x/tools/internal/typesinternal#InvalidChanAssign)

So MyOutputJobStream is not compatible with boost.OutputStream[MyOutput]

@plastikfan plastikfan added the refactor Refactor code label Aug 30, 2023
@plastikfan plastikfan self-assigned this Aug 30, 2023
@plastikfan
Copy link
Contributor Author

Actually, on the client side we can make this work by using 'type =':

type MyInputJobStream = boost.JobStream[MyInput]
type MyOutputJobStream = boost.OutputStream[MyOutput]

with these in place, we can now do:

	pool.Start(context.Background(),
		make(MyOutputJobStream),
	)

... and this works because of the '=', which doesn't introduce new types, they introduce aliases to existing type, which makes them compatible, so now MyOutputJobStream is compatible with boost.OutputStream[MyOutput]

This is a good lesson. Perhaps this should be emphasized in documentation.

@plastikfan
Copy link
Contributor Author

so now, the purpose of this issue changes from redefining the core channel types to changing the way these types are interacted with.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
refactor Refactor code
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant