14

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.

subhash kumar singh
  • 2,716
  • 8
  • 31
  • 43

2 Answers2

9

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".

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • would it be possible to know the type and value from the interface argument ? I am still trying to understand it from the shared example. – subhash kumar singh Dec 25 '14 at 23:10
  • @subh.singh not dynamically, which is why it is better to work with an array of a known interface, instead of an `interface{}`. – VonC Dec 25 '14 at 23:12
  • 1
    @subh.singh at least not without reflection, as bit like in http://play.golang.org/p/jxMFq5UYs1 mentioned in the comments of https://www.tbray.org/ongoing/When/201x/2013/07/15/Golang-Diaries-2. – VonC Dec 25 '14 at 23:14
1

Your definition of Map is some unсomplete. Usual way to declare it would have mapper method. Your example can be implemented at least this way

package main

import "fmt"

// Interface to specify something thet can be mapped.
type Mapable interface {
}


func main() {
    list_1 := []int{1, 2, 3, 4}
    list_2 := []string{"a", "b", "c", "d"}
    Map(print, list_1)
    Map(print, list_2)
}
func print(value Mapable){
fmt.Print(value)
}

// This function maps the every element for
// all []types of array.
func Map(mapper func(Mapable), list ... Mapable) {
    for _, value := range list {
        mapper(value)
    }
}

It works. Need to say it's a bit of untyped. Because no, Go has not 'generics' in Hindley-Milner sence

Uvelichitel
  • 8,220
  • 1
  • 19
  • 36
  • I think if we want to perform some operation on the the elements of the array then we have to go with reflection. In the current example we are printing the value, not doing any operation on the elements. – subhash kumar singh Dec 26 '14 at 18:27
  • the usage of interface is the key here. you can directly print without Map func. thank you @Uvelichitel – Jacky Supit Aug 05 '21 at 03:00