Methods unavailable on concrete type when used to instantiate generic in receiver

76 views
Skip to first unread message

Vinicius Hespanha Souza

unread,
Jan 23, 2026, 12:54:43 AM (yesterday) Jan 23
to golang-nuts
Good evening.

I observed this weird behavior today and wanted to see if that's somehow intended or if I found a bug. Couldn't find any other mention of it in the list but I guess there are a lot of ways to describe it.

I think the paste is pretty self-explanatory, but basically I can't call a concrete type's method if i'm inside a receiver that uses an instantiated generic with a type present in a constraint union.


Thanks in advance if anyone can clear that up for me.

Sean Liao

unread,
Jan 23, 2026, 1:53:44 AM (yesterday) Jan 23
to golang-nuts
func (h *holder[alpha]) processAlpha()

this doesn't constrain the type to alpha, you've just renamed the type constraint of your holder type to shadow the alpha type declaration.

- sean

--
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 visit https://groups.google.com/d/msgid/golang-nuts/77763fcc-b769-4b69-af54-75814aa3beb0n%40googlegroups.com.

Brian Candler

unread,
Jan 23, 2026, 3:14:17 AM (yesterday) Jan 23
to golang-nuts
I came across this too:

You can think of the declaration as being like this:

func (h *holder[alpha any]) processAlpha()

(although this is not valid syntax today). The name "alpha" gets bound to some type parameter in local scope, so it is unrelated to the "alpha" which exists outside. You can even shadow names like "string" and "int" in Go, if you want to write really confusing code.

I'm not sure if what you were trying to do is something like this:

type T = *holder[alpha]

func (h T) processAlpha() {
item := alpha(0.1)
_ = item.validate()
}

If so, that's not allowed:

./prog.go:28:9: cannot define new methods on instantiated type holder[alpha] 


Ian Lance Taylor

unread,
Jan 23, 2026, 2:48:57 PM (yesterday) Jan 23
to Vinicius Hespanha Souza, golang-nuts
I'm not sure but perhaps it will help to read
https://go.dev/doc/faq#types_in_method_declaration .

Ian
Reply all
Reply to author
Forward
0 new messages