In the second case, the type argument is inferred to be `[]float64`. The
constraint on `T` in `do` is that it has to be `[]any`. `[]float64` is not
`[]any`, the two are different types, so instantiation fails.
Note that even if you explicitly instantiate `do` to `[]any`, you still
could not pass a `[]float64` to it. That's because slices are invariant in
Go, for good reasons
<https://blog.merovius.de/posts/2018-06-03-why-doesnt-go-have-variance-in/>.

On Tue, Oct 10, 2023 at 9:01 AM Torsten Bronger <
bron...@physik.rwth-aachen.de> wrote:

> Hallöchen!
>
> The two most recent Go blog articles make we wonder why
>
>     package main
>
>     func do[T []E, E any](slice T) {
>     }
>
>     func main() {
>             var a []float64
>             do(a)
>     }
>
> is valid while
>
>     package main
>
>     func do[T []any](slice T) {
>     }
>
>     func main() {
>             var a []float64
>             do(a)
>     }
>
> is not.  The error message doe not help me.  Can someone explain
> this?  Thank you!
>
> Regards,
> Torsten.
>
> --
> Torsten Bronger
>
> --
> 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/87bkd7nern.fsf%40physik.rwth-aachen.de
> .
>

-- 
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/CAEkBMfG-4ykFgrnLR2nDYtj5NcoS4%2By5EMiYFoAb5P_uQvOd7A%40mail.gmail.com.

Reply via email to