1

I want to have a slice of structs with a generic. The generic is an interface with a type constraint.

type constraint interface {
    int32 | uint32
}

type a[T constraint] struct {
    something T
}

type b struct {
    as []a[constraint] // doesn't work
}

How do I make it so i can use both a[int32] and a[uint32] as elements in b.as?

Peirceman
  • 21
  • 4
  • You either specify a non generic type for the instantiation, e.g. `as []a[int32]`; or you make the `b` type generic and use the type parameter in the instantiation: https://go.dev/play/p/BeTEslmPSCt – mkopriva Dec 30 '22 at 19:26
  • 2
    You cannot make a slice with mixed element types if that's what you're asking. – Peter Dec 30 '22 at 19:27

1 Answers1

1

What I am trying to do is not possible in the way I am proposing. This is because a[int32] and a[uint32] are different types. I need to create an internal interface that only a implements and create an array of that.

type internal interface {
    someObscureMethod()
}

func (anA a[T]) someObscureMethod() {}

type b struct {
    as []internal
}

Peirceman
  • 21
  • 4