Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

wg.Go(func() { ... }) func support parameters #87

mocanxi started this conversation in General
Discussion options

wg.Go(func() { ... })

func() does not support parameters and has limited use

You must be logged in to vote

Replies: 1 comment 6 replies

Comment options

Can you give me a use case you want to be supported? Almost any use case I can picture can be handled by capturing a variable from the environment.

You must be logged in to vote
6 replies
Comment options

In order for this to work, doSomething needs to return a closure that will then be executed asynchronously. Defining doSomething like this is already possible:

func doSomething(task any) func() {
 return func() {
 println(task)
 }
}

Making conc work like your example is not possible. The only way to use that syntax is with the go or defer keywords.

Comment options

Ok, thank you for your answer.
It would be better if we could provide such a way :

var wg conc.WaitGroup
for _, task := range tasks {
 wg.GoParameters(doSomething,task,1)
}
wg.Wait()

Or use indefinite parameters. This way requires reflection in implementation, which is not a good way.
Anyway, thank you for your answer.

Comment options

Yeah, it's unfortunate that we can't be generic over the number of arguments. There were quite a few places where I'd really love to be able to name a type that basically means "this function returns a value and an error, or just an error". Unfortunately, that's not yet possible

Comment options

Thank you, I also understand the considerations

Comment options

I think this would be possible. But if the parameters wouldn't match the function definition you would get error at runtime instead of at compilation time which is not really good experience.

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

AltStyle によって変換されたページ (->オリジナル) /