[]int does not implement []any

399 views
Skip to first unread message

Michel Casabianca

unread,
May 11, 2022, 11:45:44 AM5/11/22
to golang-nuts
Hi gophers!

This code https://go.dev/play/p/QHIxegJn3aj doesn't compile. I don't understand why. Anyone would have an explanation? And why this code https://go.dev/play/p/PCSzRkLvXhp fixes it?

Thanks for your replies
Best regards
--
Michel Casabianca

Axel Wagner

unread,
May 11, 2022, 11:56:56 AM5/11/22
to Michel Casabianca, golang-nuts
When you write [S []any], this means "S must have type []any". `[]int` has type `[]int`, which is different from `[]any`, so this doesn't work - and it can't work:

When you write [E any, S []E], you say "E must be any type and S must have type []E". This *can* be instantiated using `[]int`, because `int` can take the place of `E` (it is "any type") and then `[]int` has type `[]E` and everything is great.

--
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/18738918-be16-4b0b-825e-e01f7d9c6225n%40googlegroups.com.

Michel Casabianca

unread,
May 12, 2022, 5:01:54 AM5/12/22
to Axel Wagner, golang-nuts
Thank you very much Axel for this crystal clear explanation.

Best regards
--
Michel Casabianca
Reply all
Reply to author
Forward
0 new messages