If you don't mind me extending the topic a bit: I bumped into a similar issue, which I described here:
https://stackoverflow.com/questions/73591149/generics-type-inference-when-cascaded-calls-of-generic-functions One of the solutions for the original question would be to use something like: type MultiDimensionSlices[T any] interface{ T | []T | [][]T | [][][]T ... } But it also fails with `term cannot be a type parameter`. Is this a temporary limitation (to be fixed in some future Go compiler), or fundamentally it doesn't work for some reason ? Also if I remove the "scalar" version (so remove T from the constraint types list), and define: type MultiDimensionSlices[T any] interface{ []T | [][]T | [][][]T ... } type Number interface { int | float32 } func rank[S MultiDimensionSlices[T], T Number](value MultiDimensionSlices) int {...} I cannot simply call: rank([][]float32{{1}}) // == 2 because it's not able to infer the type. Instead I'm forced to do: rank[[][]float32, float32]([][]float32{{1}}) Any way to have the automatic type inference work in this case ? Thanks!! ps.: Playground link https://go.dev/play/p/Z4O5iXwGtt6 On Friday, August 12, 2022 at 4:00:48 PM UTC+2 axel.wa...@googlemail.com wrote: > Indeed. In hindsight, I should've predicted that. > I think you are SOOL then, in general. I'd suggest using ~[]E directly and > do the "scalar" case as a 1-element slice then. > > For a function, you can write `func F[E any](e ...E)`, which makes it > callable with any number of arguments. Or `func F[E any](e E, es ...E)`, > which guarantees that there is at least one argument. > > It's probably doesn't seem ideal, but it's the only thing that works right > now. > > On Fri, Aug 12, 2022 at 3:49 PM TheDiveO <harald....@gmx.net> wrote: > >> Gave it a try on playground, but this unfortunately raises the error >> "term cannot be a type parameter" for the E | ~[]E. >> >> On Friday, August 12, 2022 at 3:18:05 PM UTC+2 axel.wa...@googlemail.com >> wrote: >> >>> 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....@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...@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...@googlegroups.com. >> > To view this discussion on the web visit >> https://groups.google.com/d/msgid/golang-nuts/a0c2e8bb-d1bf-4fd0-ac2e-6b062be613e9n%40googlegroups.com >> >> <https://groups.google.com/d/msgid/golang-nuts/a0c2e8bb-d1bf-4fd0-ac2e-6b062be613e9n%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/9ffba2d5-0615-4317-9b7e-2d834f25abbdn%40googlegroups.com.