I don't know what you are doing, here, but… func some() string { foo := struct{ Key string, Value int }{} // some code
// compie error: Can't use foo.Key (type string) as int acceptInt(foo.Key) // compile error: mismatched types string and int if foo.Key == foo.Value { } // compile error: Can't use foo.Value (type int) as string return foo.Value } perfectly type safe. On Wed, Jun 22, 2016 at 6:21 AM, <andrew.mez...@gmail.com> wrote: > >> The issue is, that a "KeyValuePair<K, V>" (no matter if you implemented > it via generics or like you mention via interfaces) is a fundamentally > useless type and generics encourage people to add useless types. A > "KeyValuePair<K, V>" is a "struct { Key K, Value V }", plain and simple. > It's not an interface and it's not a generic type, it's simply a struct. > > I not agree with you only because with generics this pattern is always > type safe. > > Eg. > > type Foo<K, V> struct { > > key K > val V > } > func accepInt(i int) { > } > func some() string { > foo := &Foo<string, int>() > // some code > > // Compile error: `key` is not `int` > acceptInt(foo.key) > > // Compile error: `key` (int) cannot be compared with `string` > if foo.key == foo.val { > } > > // Compile error: `key` is not `string` > return foo.val > } > func baz(foo Foo<K, V>) bool { > // Compile error: `key` (K) cannot be compared with `V` > return foo.key == foo.val > } > > > -- > 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. > For more options, visit https://groups.google.com/d/optout. > -- 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. For more options, visit https://groups.google.com/d/optout.