What is the difference between comparable and any?

Issue

I have tried to use generics with Go, but I don’t really understand when we use any or comparable as type parameter. Can someone help to understand these?

Solution

It depends on what / how you want to use values of the parameter type. Constraints restrict what you can do with values of those types.

any is an alias for interface{} which allows any type. If a parameter can be of any type, that basically won’t allow you to do anything with it because you have no guarantee what it will be.

The comparable constraints only allows types that are comparable, that is, the == and != operators are allowed to use on values of them. This is good if you want to use the type as a key in a map (maps require key types to be comparable), or if you you want to find an element in a slice, and you want to use the == operator to compare the elements to something.

As an example, let’s write a generic map-get function:

func get[K comparable, V any](m map[K]V, key K) V {
    return m[key]
}

The K key type must be comparable, else it cannot be used as the key type of some map (m[K]V in the example). V on the other hand shouldn’t be constrained, the value type may be anything, and we’re not doing anything with it (just returning a value of type V), so using any here is the best choice.

Another example, a slice-find function:

func find[V comparable](what V, s []V) int {
    for i, v := range s {
        if v == what {
            return i
        }
    }
    return -1
}

find() returns the index of the first occurrence of what in the slice s, and if it’s not in the slice, returns -1. The type parameter V here must be comparable, else you couldn’t write v == what, using V any would be a compile-time error. The constraint comparable ensures this find() function can only be instantiated with types (and called with values) where the == operator is defined and allowed.

Answered By – icza

Answer Checked By – Marilyn (GoLangFix Volunteer)

Leave a Reply

Your email address will not be published.