I feel like https://go2goplay.golang.org/p/RLn9BXjU1OR is a better compromise than having two functions.
On Tue, Aug 4, 2020 at 5:50 PM 'Axel Wagner' via golang-nuts < [email protected]> wrote: > No, that's not possible. A generic function can only work with *either* > built in operators *or* methods. So, you always need to have two functions: > https://go2goplay.golang.org/p/YuzSzDAOEo2 > > > On Tue, Aug 4, 2020 at 4:32 PM Juliusz Chroboczek <[email protected]> wrote: > >> I'd be grateful if people could give me an example to help me understand >> the generics draft. Suppose I've got these two functions: >> >> func MemberInt(x int, a []int) bool { >> for _, v := range a { >> if v == x { >> return true >> } >> } >> return false >> } >> >> func MemberIP(x net.IP, a []net.IP) bool { >> for _, v := range a { >> if v.Equal(x) { >> return true >> } >> } >> return false >> } >> >> I can see how to write a generic "Member" function that takes an extra >> equality predicate; but is there a way to write a function that >> generalises both functions above without requiring the extra parameter? >> >> Thanks. >> >> -- >> You received this message because you are subscribed to the Google Groups >> "golang-nuts" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to [email protected]. >> To view this discussion on the web visit >> https://groups.google.com/d/msgid/golang-nuts/87v9hytrcn.fsf%40pirx.irif.fr >> . >> > -- > You received this message because you are subscribed to the Google Groups > "golang-nuts" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > To view this discussion on the web visit > https://groups.google.com/d/msgid/golang-nuts/CAEkBMfHBjg%3Dp7HTOAfVx-q9vvnJsdcQ0ozRt_Uwsn2yF2nW6jg%40mail.gmail.com > <https://groups.google.com/d/msgid/golang-nuts/CAEkBMfHBjg%3Dp7HTOAfVx-q9vvnJsdcQ0ozRt_Uwsn2yF2nW6jg%40mail.gmail.com?utm_medium=email&utm_source=footer> > . > -- Andrei Călin -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CANnvmN5ELU%2BOz1tk-S5-V2Mh6hKRnJK7uRT5wjehs5M8DgO5pQ%40mail.gmail.com.
