[generics] Allowing interfaces with type constraints

186 views
Skip to first unread message

Viktor Kojouharov

unread,
Sep 10, 2020, 10:53:59 AM9/10/20
to golang-nuts

Would using parametric interfaces be allowed in type assertion expressions / type switches?

The current behavior in the go2go playground is for the compiler to return the error: "interface contains type constraints (T)" (https://go2goplay.golang.org/p/O1Un2Vm9Dh0)
I was under the impression that since the outer function is already parametrised with the same type, this should work without a problem.

Perhaps I'm missing something, but I currently don't see how one can check for optional interface implementations of generic types, since this error will pretty much stop them.

roger peppe

unread,
Sep 10, 2020, 11:17:57 AM9/10/20
to Viktor Kojouharov, golang-nuts
You can definitely use parametric interfaces in type assertion expressions, but you can't use interfaces with type lists.
I'm not sure exactly what you're trying to do in your example, but ISTM that it could be somewhat simpler. You
only need one type parameter: https://go2goplay.golang.org/p/4E0ZnVxJwL9


--
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/fbe50aaf-1794-4a17-9321-2b923f546d13n%40googlegroups.com.

Viktor Kojouharov

unread,
Sep 10, 2020, 11:41:50 AM9/10/20
to golang-nuts
I see,

I guess the compiler error message needs improvement here. It could mention that interfaces with type lists cannot be used.
Reply all
Reply to author
Forward
0 new messages