Re: [go-nuts] Generics type interference on "T | []T" to get T?

2022-08-12 Thread 'Axel Wagner' via golang-nuts
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

Re: [go-nuts] Generics type interference on "T | []T" to get T?

2022-08-12 Thread TheDiveO
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

Re: [go-nuts] Generics type interference on "T | []T" to get T?

2022-08-12 Thread 'Axel Wagner' via golang-nuts
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 wrote: > I would like to provide a generics-based type-safe API that accepts either > a scalar or slice of some type T

[go-nuts] Generics type interference on "T | []T" to get T?

2022-08-12 Thread TheDiveO
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)...

Re: [go-nuts] Pipelining with Go and Generics

2022-08-12 Thread Robert Engels
Maybe there is a language barrier. My statement implied Go was a modern language yet it did not have a streams package - not that it was not a modern language. You seem to be looking for a fight and I’m not sure why. Why don’t you focus on solutions - it’s better for everyone. > On Aug 12,

Re: [go-nuts] Pipelining with Go and Generics

2022-08-12 Thread Jan Mercl
On Thu, Aug 11, 2022 at 11:18 PM Robert Engels wrote: > Every modern language with generics or similar has a streams package so > there’s no need to call out Java. For some reason unknown to me you seem to consider Go not being a modern language as it does not have a "streams" package. I don't

Re: [go-nuts] Type switch on generic parameter

2022-08-12 Thread roger peppe
See https://github.com/golang/go/issues/45380 for a proposal for a language feature that would allow this. In the meantime, you can convert to any, and type switch on that: func Foo[T allowedTypes](arg T) { switch t := any(arg).(type) { case int64: ... On Fri, 12 Aug 2022, 00:40 'Matt