> Is this possible in Go? To have a "wildcard" type parameter? If not, what 
> would be an alternative to the above?

I don’t believe so. That would require pattern matching on constructors, which 
Go doesn’t have.

It seems to me you can either assign msg to a non-generic interface and then 
type-switch over non-generic type possibilities, or invoke methods on msg that 
implement the type switch cases.

In my opinion, parametrically-polymorphic (generic) code is supposed to behave 
the same regardless of the instantiated type, so you’re probably heading off 
into the weeds with this approach. If you want code to behave differently based 
on the concrete type (ad-hoc polymorphism), that’s what interfaces are for. 
Don’t mix the two in the same place.

Will

> On Nov 23, 2025, at 4:09 AM, Matthias Jeschke <[email protected]> wrote:
> 
> Hi, 
> 
> I'm experimenting with bubbletea for creating a TUI and I came across the 
> following problem:
> 
> I have a generic struct declared:
> 
> type ResourceUpdateMsg[T any] struct {
>       value T
> }
> 
> This is used to create messages, e.g.:
> 
> func updateCpuStats() tea.Msg {
>       cpuStats, _ := cpu.Percent(0, false)
>       return ResourceUpdateMsg[Cpu]{
>               Cpu{
>                       Usage: cpuStats[0],
>               },
>       }
> }
> 
> Now, in one update function I'm not interested in the specific type of a 
> message; each message should be routed to another, more specific update 
> function:
> 
>       switch msg := msg.(type) {
> 
>       case resources.ResourceUpdateMsg[any]:
>               resourceModel, cmd := m.resources.Update(msg)
>               m.resources = resourceModel.(resources.Model)
>               return m, cmd
>          ....
>          }
> 
> In the other, specific update function the switch distinguishes between 
> different types:
> 
>       switch msg := msg.(type) {
>       case ResourceUpdateMsg[Cpu]:
>               m.Cpu = msg.value
>               cmd := m.Progress.CpuProgress.SetPercent(m.Cpu.Usage / 100)
>               return m, tea.Batch(tickCmd(updateCpuStats), cmd)
>       case ResourceUpdateMsg[Mem]:
>               m.Mem = msg.value
>               cmd := m.Progress.MemProgress.SetPercent(m.Mem.Usage / 100)
>               return m, tea.Batch(tickCmd(updateMemoryStats), cmd)
>         ...
>         }
> 
> But this does not work, the case is never selected in the first update 
> function. I have to write all cases, but each case basically does the same: 
> route the message to the other function.
> 
> I'm fairly new to Go and my background is Java / Kotlin. In those languages I 
> would work with inheritance.
> 
> Is this possible in Go? To have a "wildcard" type parameter? If not, what 
> would be an alternative to the above?
> 
> 
> -- 
> 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 [email protected] 
> <mailto:[email protected]>.
> To view this discussion visit 
> https://groups.google.com/d/msgid/golang-nuts/11bbc330-ef43-427e-97c4-829052fbc9dfn%40googlegroups.com
>  
> <https://groups.google.com/d/msgid/golang-nuts/11bbc330-ef43-427e-97c4-829052fbc9dfn%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 [email protected].
To view this discussion visit 
https://groups.google.com/d/msgid/golang-nuts/E234DED4-08F6-44E9-AAC5-F14AD5331C02%40willfaught.com.

Reply via email to