On Sun, Jul 5, 2020 at 3:43 PM, Martin Tournoij <mar...@arp242.net> wrote:

>
> So looks like that's not supported, fait enough, but the error message
> is a bit confusing. Second try:
>
>         func showAll(type enum Fruit)(fruits []enum) {
>                 for _, f := range fruits {
>                         show(f)
>                 }
>         }
>
>         func main() {
>                 // Fruit does not satisfy Fruit (interface{type Banana,
> Coconut} not found in Banana, Coconut)
>                 showAll([]Fruit{Banana{}})
>         }
>
> I'm not entirely sure what to make of that error 🤔
>

Fruit isn't one of the types you listed. You only listed Banana and
Coconut. In principle, you'd had to also list Fruit in order for it to
work. Unfortunately, actually using a interface like this isn't supported
(you get a fairly opaque error, but the reason is because you aren't
allowed to have values of type Fruit, since Fruit is an interface
containing a type list):

https://go2goplay.golang.org/p/KFaRON-Kywi

It seems like it wouldn't really hurt to be allowed to have values of type
Fruit, it just wouldn't be terribly useful. Aside from the static guarantee
about what types can be held by the interface, it's equivalent to type
interface{}.

One thing I want to point out is that you keep running into trying to use a
type parameter (enum, in your case) to represent more than one type at
once. For each instantiation of a parameterized function, the type
parameter has to represent exactly one specific type.

In my example above, I provided the explicit parameter in each case, as in
 PrintFruit(Banana) vs. PrintFruit(Coconut). This works basically as if you
replaced every appearance of F in PrintFruit with either Banana or Coconut.
You can't have both Banana and Coconut values being used as type F at the
same time. And the definition of PrintFruit doesn't know which particular
type will be passed in, so it doesn't know if F is either Banana or
Coconut, so it can't treat F as being the same as either.

-- 
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/CANjmGJuyXhV3yvYADfemjsaitBn2%3DN8N1FNsadx3dPDoaEfA2w%40mail.gmail.com.

Reply via email to