Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to make a function with inferred nillable comparable generics?

Consider the following function:

func NilCompare[T comparable](a *T, b *T) bool {
    if a == nil && b == nil {
        // if both nil, we consider them equal
        return true
    }
    if a == nil || b == nil {
        // if either is nil, then they are clearly not equal
        return false
    }
    return *a == *b
}

This function works. However, when I call it, I must supply the type, as Go cannot infer (cannot infer T) it, e.g. NilCompare[string](a, b), where a and b are *string.

If I modify T to be *comparable and a and b to be T, I get this error instead:
cannot use type comparable outside a type constraint: interface is (or embeds) comparable

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I am using Go 1.19.2.

$ go version
go version go1.19.2 linux/amd64

Ironically, my IDE (GoLand 2022.2.3) believes that the above function should be inferrable.

Is there a way to make a function that take nillable comparable and make it inferrable? Or am I doing it correct, but I need to help the go function along?

>Solution :

Type inference just works, in this case. You simply can’t infer T using literal nil, as NilCompare(nil, nil) because that doesn’t really carry type information.

To test your function with nils do this:

package main

import "fmt"

func main() {
    var a *string = nil
    var b *string = nil
    // a and b are explicitly typed
    res := NilCompare(a, b) // T inferred
    fmt.Println(res) // true
}

this also would work:

func main() {
    // literal nil converted to *string
    res := NilCompare((*string)(nil), (*string)(nil)) // T inferred
    fmt.Println(res) // true
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading