type ScalarOrSlice[E any] interface { E | ~[]E } func New[E any, T ScalarOrSlice[E]](v T) { … }
Though I haven't tested this. On Fri, Aug 12, 2022 at 3:07 PM TheDiveO <harald.albre...@gmx.net> wrote: > I would like to provide a generics-based type-safe API that accepts either > a scalar or slice of some type T (not a type parameter), that is, the type > constraint is (if I'm not mistaken) "T | []T". > > While I understand that this constraint would be declared as an interface > (simplified example)... > > type interface ScalarOrSlice { int | []int } > > ...the straightforward generics-powered func definition... > > func New[E ScalarOrSlice](e E) { ... } > > either gives me "int" or "[]int". Is it somehow possible to derive the > "int" type at compile time so that I have an, say, "F" that is always the > scalar type of E? > > Thanks, TheDiveO > > -- > 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/0ae02f54-67d6-4070-a11f-36c31a961598n%40googlegroups.com > <https://groups.google.com/d/msgid/golang-nuts/0ae02f54-67d6-4070-a11f-36c31a961598n%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/CAEkBMfHViNgzOVFTYa6DvXQC8MaA%3DQgf9LaHsmhMd4NvUn0_3w%40mail.gmail.com.