[go-nuts] Re: generics: what can we do with an any type?

2021-12-16 Thread Howard C. Shaw III
The code you wrote is using a generic value, but it is not itself generic. There is only one instantiated version of the runInt() function, and it knows (at compile-time!) that mySet is an map[int]int. Make that function actually generic - something like: func testThing[K comparable, V any](m

[go-nuts] Re: generics: what can we do with an any type?

2021-12-16 Thread w54n
[]byte is a no comparable type. quoting the docs: *comparable is an interface that is implemented by all comparable types(booleans, numbers, strings, pointers, channels, interfaces,arrays of comparable types, structs whose fields are all comparable types).The comparable interface may only

[go-nuts] Re: generics: what can we do with an any type?

2021-12-15 Thread Brian Candler
NewThing[int, int]() returns a map[int]int, and therefore that's the type of MySet in func runInt. You can think of NewThing[T1, T2] as a family of functions, like func NewThing_int_int() map[int]int { ... } func NewThing_int_byteslice() map[int][]byte { ... } ... etc On Wednesday, 15

[go-nuts] Re: generics: what can we do with an any type?

2021-12-15 Thread Sean Liao
when writing type parameterized code, it has to work with anything that could be passed to it. but once instantiated, the type is known and it can be used with its concrete type like `map` can be thought of as declared to be `map[comparable]any` but when you use it, the type is always known,