看完 Philip Wadler 关于轻量级围棋的演讲后,我对最新的 go generics draft 感到非常兴奋.但是现在有了一个可供我们使用的新泛型草案版本,似乎轻量级围棋中的一些东西不再可能。在这两个 talk ,以及 paper他引入了一个类似仿函数的接口(interface),称为 List
.论文中的方法不太奏效。
type Any interface {}
type Function(type a Any, b Any) interface {
Apply(x a) b
}
type Functor interface {
Map(f Function) Functor
}
失败并出现错误:
cannot use generic type Function(type a, b) without instantiation
如果您尝试将类型参数添加到方法中,并使用普通函数,您会得到:
methods cannot have type parameters
我想知道是否有人找到了使仿函数与当前版本的草案一起使用的方法。
最佳答案
您还没有使用泛型类型;在您的示例代码中,Functor
正在治疗Function
好像它不是通用的。正确的代码(编译, see here )将是:
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)
}
关于go - 在泛型 go 中模拟方法类型参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62456946/