0
votes

I'm relatively new to go. I'm trying to write a generic "appender" function. This is a simplification, but its an attempt to create a clean interface for processing some lists. Specifically, I have questions about the two errors that this generates:

package main

type GenericFunc func() *interface{}
func Append(ints interface{}, f GenericFunc) {
    ints = append(ints, f())
}

func ReturnInt() *int {
    i := 1
    return &i
}

func main() {
    var ints []*int
    Append(ints, ReturnInt)
}

Playground

prog.go:5:18: first argument to append must be slice; have interface {} prog.go:15:11: cannot use ReturnInt (type func() *int) as type GenericFunc in argument to Append

  1. Why can't ReturnInt be of type GenericFunc? If this doesn't work, I'm not understanding how interface{} can be used with functions at all.. can it?
  2. How can you accept a "generic" slice and append to it using reflection? This would involve checking that GenericFunc returns the same type that the slice is, but after that appending should be possible.
1
Why not just use append? It is already generic. For example you can do this ints = append(ints, ReturnInt()). Also note that, apart from a few builtins, Go does not support generics, and therefore an attempt to implement generic types in your go program is gonna result in either failure or a solution that's lacking. - mkopriva
Why the downvote? I'm well aware of the generics situation in go, but there are appropriate times to use it. In this case, its a single, very isolated, small function that can unlock a lot of other cleanliness and go-like idioms throughout the rest of my application. Feels like the appropriate level of tradeoff for me. - JaredC
Jared just FYI, I'm not the one who gave you the downvote, so I can't answer your question. As to the rest of your comment: fair enough. - mkopriva

1 Answers

2
votes

The types func() *interface{} (type type of GenericFunc) and (type func() *int) (the type of ReturnInt) are different types. One returns a *interface{}. The other returns a *int. The types are not assignable to each other.

Use this function to generically append the result of a function to a slice:

func Append(sp interface{}, f interface{}) {
    s := reflect.ValueOf(sp).Elem()
    s.Set(reflect.Append(s, reflect.ValueOf(f).Call(nil)[0]))
}

Call it like this:

var ints []*int
Append(&ints, ReturnInt)

The function will panic if the argument is not a pointer to a slice or the function does not return a value assignable to a slice element.

playground example