Hi community,
I have got this now. It handles only built-in types: string, int, float64. The tuples (slices) should be the same size. Type of elements in same position should be the same. I still need help on this: The part involves cmp.Compare repeates three times for string, int, float64. I wonder if I can call the type assertion with a type ` T ` like: a[i].(T), where T is from reflect. so, i can do the part with three calls, not repeat the part three times. I tried but a[i].(T) will not take T as reflect.TypeOf. s := []any{"", 0, 0.0} for _, element := s { t := reflect.TypeOf(element) v, ok := element.(t) // .(string), .(int), .(float64) } Is this ability same as c++ template meta programming or rust macro. How can i do it in Go? Thanks ``` package tuple2 import ( "cmp" "reflect" ) func Cmp(a, b []any) (int, bool) { if len(a) != len(b) { return 0, false } for i := range a { if a[i] == nil || b[i] == nil { return 0, false } if _, boolean := a[i].(bool); boolean { return 0, false } if _, boolean := b[i].(bool); boolean { return 0, false } if a, b := reflect.TypeOf(a[i]), reflect.TypeOf(b[i]); a != b { return 0, false } if a, aOk := a[i].(string); aOk { if b, bOk := b[i].(string); bOk { if c := cmp.Compare(a, b); c != 0 { return c, true } } } if a, aOk := a[i].(int); aOk { if b, bOk := b[i].(int); bOk { if c := cmp.Compare(a, b); c != 0 { return c, true } } } if a, aOk := a[i].(float64); aOk { if b, bOk := b[i].(float64); bOk { if c := cmp.Compare(a, b); c != 0 { return c, true } } } } return 0, true } /* func main() { a := []any{"abc", 123, 3.14} b := []any{"abc", 123, 3.14} c, ok := tuple2.Cmp(a, b) fmt.Println(c, ok) } */ ``` -- 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 golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/tencent_234F3EB67D28257F237804E3D83873722905%40qq.com.