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, eg. `map[string]int` On Wednesday, December 15, 2021 at 5:39:39 PM UTC+1 Johannes Kohnen wrote: > Hello folks, > > I've stumbled over a thing and I don't understand if what I've found is > intentionally possible by design... > > https://go.dev/play/p/SIxOV1FnTzX?v=gotip > > Why can I compare int with == when it was instantiated from the "any" type > parameter? > > The origin of my question is my implementation of a left right map which > I've ported to generics. While doing so I've uncovered a couple bugs in > Jetbrains GoLand and reported them. Except this one, because I've > overlooked it and I now am confused: > > > https://github.com/jwkohnen/lrmap/blob/a6b9a6715069a7ad24de7cf84fe949dd7f194c0a/lrmap_test.go#L26 > > In this line I compare two int values with !=, and this code compiles. > Though, those value types were instantiated from the "any" type > constraint. GoLand reports an error in that line "Invalid operation: _v != > v (the operator != is not defined on V)". > > From my reading of the generics proposal GoLand is right, but the compiler > says otherwise. How exactly are the operations that are defined on an > "any" type inferred? > > Sorry if the answer is right there in the proposal; my reading > comprehension fluctuates wildly these days. > > Cheers, > Johannes > -- 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/721e2328-e7ab-48a8-80fc-bf11a783d187n%40googlegroups.com.