While exercising Go with various Future implementations, at one moment reflection hit me. Using reflection to inspect types beats the point of strong types, but it never hurts to play.
I want to use any function within a Future and then call it asynchronous and wait for results. I could have a function to create the Future which holds the user function. Not having generics yet, any function could be passed by interface{}, then called by reflection.
So the user function is passed to a function which returns another function that can be called with the user function arguments. The user function will be executed on a new routine.
// Callable is the function to call in order to start running the passed function. type Callable func(args ...interface{}) *Future // New returns a Callable. func New(function interface{}) Callable {
For a sum function, the usage is:
sum := func(a, b int) (int, error) { return a + b, nil } future := futurereflect.New(sum)(1, 2) result, err := future.Result()
The Future should allow waiting for the user function to be executed and get its result.
type Future struct { functionType reflect.Type functionValue reflect.Value args []interface{} wait chan struct{} result interface{} err error } // Wait blocks until function is done. func (f *Future) Wait() { <-f.wait } // Result retrieves result and error. It blocks if function is not done. func (f *Future) Result() (interface{}, error) { <-f.wait return f.result, f.err }
The rest is just implementation, but it feels like pure madness. I did it for fun, to experiment, there are cases missing. And a language should not be pushed where it doesn’t belong. Use each language in its style.
Run the code on Go Playground.