[SOLVED] How to write func for the generic parameter in golang

Issue

I am trying to write a function Map, so that it can handle all the types of array.

// Interface to specify generic type of array.
type Iterable interface {
}

func main() {
    list_1 := []int{1, 2, 3, 4}
    list_2 := []uint8{'a', 'b', 'c', 'd'}
    Map(list_1)
    Map(list_2)
}

// This function prints the every element for
// all []types of array.
func Map(list Iterable) {
    for _, value := range list {
        fmt.Print(value)
    }
}

But it throws the compile time error.

19: cannot range over list (type Iterable)

The error is correct because range require array, pointer to an array, slice, string, map, or channel permitting receive operations and here type is Iterable. I think problem that I am facing is, conversion of the argument type Iterable to array type. Please suggest, how could I use my function to handle generic array.

Solution

As Rob Pike mentions in this thread

Is it possible to express "any map", "any array" or "any slice" in a Go type switch?

No. The static types must be exact.
The empty interface is really a type, not a wildcard.

You only could iterate over a list of a specific type, like an interface with known functions.
You can see an example with "Can we write a generic array/slice deduplication in go?"

Even using reflection, to pass a slice as an interface{} would be, as this thread shows, error-prone (see this example).


Update Nov. 2021, 7 years later: CL 363434, for Go 1.18 (Q1 2022) actually introduces functions useful with slices of any type, using generics.

// Package slices defines various functions useful with slices of any type.
// Unless otherwise specified, these functions all apply to the elements
// of a slice at index 0 <= i < len(s).
package slices
import "golang.org/x/exp/constraints"
// Equal reports whether two slices are equal: the same length and all
// elements equal. If the lengths are different, Equal returns false.
// Otherwise, the elements are compared in index order, and the
// comparison stops at the first unequal pair.
// Floating point NaNs are not considered equal.
func Equal[T comparable](s1, s2 []T) bool {
    if len(s1) != len(s2) {
        return false
    }
    for i, v1 := range s1 {
        v2 := s2[i]
        if v1 != v2 {
            return false
        }
    }
    return true
}

Note that issue 50792 and CL 382834 show that:

We left constraints behind in the standard library because we believed it was fundamental to using generics, but in practice that hasn’t proven to be the case.

In particular, most code uses any or comparable.
If those are the only common constraints, maybe we don’t need the package.
Or if constraints.Ordered is the only other commonly used constraint, maybe that should be a predeclared identifier next to any and comparable.

Hence import "golang.org/x/exp/constraints" instead of import "constraints".

Answered By – VonC

Answer Checked By – Senaida (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *