The uint8 in cachedFn[uint8, V] is the type parameter name, it can be arbitrary valid identifier. It shadows the predeclared uint8 identifier.
On Friday, November 10, 2023 at 12:25:05 PM UTC+8 ahuigo wrote: > Btw, please ignore the type logic in my code, I wrote this piece of code > just to illustrate the oddities of generics in struct methods. > Regardless of whether its type is int, string, any, *or the exact uint8, > this error is very strange.* > > > // it doesn't work > func (c *cachedFn[uint8, V]) Get0() (V, error) { > var s uint8 = 0 > s = 0 // error: cannot use 0 (untyped int constant) as uint8 value in > assignment > fmt.Printf("cache key: %#v, %T\n", s, s) // cache key: 0, uint8 > return c.Get(s) > } > > // it works > func (c *cachedFn[uint8, V]) Get0() (V, error) { > var s uint8 = 0 > fmt.Printf("cache key: %#v, %T\n", s, s) // cache key: 0, uint8 > return c.Get(s) > } > On Friday, November 10, 2023 at 4:34:46 AM UTC+8 Axel Wagner wrote: > >> Yes, this has come up before. >> >> On Fri, Nov 10, 2023 at 7:09 AM ahuigo <a13...@gmail.com> wrote: >> >>> There is an example: https://go.dev/play/p/guzOWRKi-yp >>> >>> ``` >>> func (c *cachedFn[string, V]) Get0() (V, error) { >>> // var s any >>> var s string >>> s = "abc" // error: cannot use "abc" (untyped string constant) as string >>> value in assignment >>> fmt.Printf("cache key: %#v, %T\n", s, s) // cache key: 0, uint8 >>> return c.Get(s) >>> } >>> ``` >>> I find the generic type of the struct method a bit confusing. >>> 1. The type `cachedFn[string, V]` does not really constrain the type >>> of `s` to **string**. It's actual type is `uint8` >>> >> >> The type `cachedVn[string, V]` *would* in fact instantiate `cachedVn` >> with `string` and `V`. >> But that's not what you are doing. You are writing the receiver type as >> `fun c(c *cachedFn[string, V])`, which means that "the receiver is the >> generic type `cachedVn` with two type parameters called `string` and `V`". >> Predeclared identifiers in Go are not special in any way, you can re-use >> them for your own variables and types - or type parameters. So what you are >> doing here is fundamentally similar to this problem: >> https://go.dev/play/p/lDE-o7fGHi8 >> >> There probably should be a vet check for using a predeclared identifier >> as a type parameter name (or maybe even for any re-use of a predeclared >> identifier). >> >> 2. And this error is a bit strange. (`s = "abc" // error: cannot use >>> "abc" (untyped string constant) as string value in assignment. ` ) >>> >>> >>> -- >>> 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...@googlegroups.com. >>> To view this discussion on the web visit >>> https://groups.google.com/d/msgid/golang-nuts/58c92577-cb98-401a-978d-c22a1fb493ccn%40googlegroups.com >>> >>> <https://groups.google.com/d/msgid/golang-nuts/58c92577-cb98-401a-978d-c22a1fb493ccn%40googlegroups.com?utm_medium=email&utm_source=footer> >>> . >>> >> -- 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/3b7f2865-f225-4e5e-ae0e-7edf5bb333d6n%40googlegroups.com.