-
Notifications
You must be signed in to change notification settings - Fork 358
-
wg.Go(func() { ... })
func() does not support parameters and has limited use
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 1 comment 6 replies
-
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.
Beta Was this translation helpful? Give feedback.
All reactions
-
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.
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 2
-
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.
Beta Was this translation helpful? Give feedback.
All reactions
-
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
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 1
-
Thank you, I also understand the considerations
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 1
-
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.
Beta Was this translation helpful? Give feedback.