Search code examples
gogenericsfunctor

emulating method type parameters in generic go


After watching Philip Wadler's talk on featherweight go I was really excited about the newest go generics draft. But now with a version of the new generics draft available for us to play with it seems some of the things from featherweight go are no longer possible. In the both the talk, and the paper he introduces a functor like interface called List. The approach from the paper doesn't quite work.

type Any interface {}

type Function(type a Any, b Any) interface {
    Apply(x a) b
}

type Functor interface {
   Map(f Function) Functor
}

fails with the error: cannot use generic type Function(type a, b) without instantiation

and if you try to add type parameters to the method, and use a normal function you get: methods cannot have type parameters

I'm wonder if anyone has found an approach to making functors work with the current version of the draft.


Solution

  • You haven't carried the generic types through; in your example code, Functor is treating Function as if it were not generic. The correct code (which compiles, see here) would be:

    type Function(type a Any, b Any) interface {
        Apply(x a) b
    }
    
    type Functor(type a Any, b Any) interface {
       Map(f Function(a,b)) Functor(a,b)
    }