Yes, this has come up before.

On Fri, Nov 10, 2023 at 7:09 AM ahuigo <a132...@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+unsubscr...@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/CAEkBMfGJBR7QwNDyeCvp10mDhRTeGnk0T01OFq01GvgDSx8KgA%40mail.gmail.com.

Reply via email to