generics go2go Type List in Interface - Struct Type

68 views
Skip to first unread message

Rich Moyse

unread,
Aug 11, 2020, 4:59:08 PM8/11/20
to golang-nuts

Attempting to use a Type List in an Interface to restrict the types allowed to instantiate a generic function (example in go2go).  The Type List within the Constraint Interface (Santa) contains only one struct type (SantaSad).  This struct type and a second, independent struct type (SantaHappy) both implement the Constraint Interface (Santa) specified as a type parameter for the generic function.  Given this situation, I expected the instantiation of the generic function with a type argument of the type (SantaSad), that appears in the Constraint's Interface Type List to succeed, which it does, and instantiating the generic function with the struct type, absent from the Type List (SantaHappy), to fail.  However, instead of failing, this second case succeeds.  Is this the intended behavior?


Axel Wagner

unread,
Aug 11, 2020, 5:14:00 PM8/11/20
to Rich Moyse, golang-nuts
Yes, it's intended. From the draft:

More precisely, the underlying type of the type argument must be identical to the underlying type of one of the types in the type list.

Type-lists match by underlying type of both argument and the types in the type-list. As SantaHappy and SantaSad both have underlying type `struct{}`, they are the same.

On Tue, Aug 11, 2020 at 10:59 PM Rich Moyse <ri...@moyse.us> wrote:

Attempting to use a Type List in an Interface to restrict the types allowed to instantiate a generic function (example in go2go).  The Type List within the Constraint Interface (Santa) contains only one struct type (SantaSad).  This struct type and a second, independent struct type (SantaHappy) both implement the Constraint Interface (Santa) specified as a type parameter for the generic function.  Given this situation, I expected the instantiation of the generic function with a type argument of the type (SantaSad), that appears in the Constraint's Interface Type List to succeed, which it does, and instantiating the generic function with the struct type, absent from the Type List (SantaHappy), to fail.  However, instead of failing, this second case succeeds.  Is this the intended behavior?


--
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/44b6cb78-d143-437f-b4e0-f1cf8d642989n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages