使用具有多个类型参数的类型类

Using typeclasses wtih multiple type parameters

我正在尝试使用 symulacrum@typeclass 来避免编写 Ops/Syntax 样板文件。我有一个用 effect 和 type 参数化的特征:

@typeclass trait Close[F[_], T]{
    def close(t: T): F[Unit]
}

意向如下:

trait Stream[F[_], Pipe]{
    def open(): F[Pipe]
    def drain(): F[Unit]
}
object App{
    def runApp[F[_], Pipe: Close[F, ?]](implicit stream: Stream[F, Pipe]) = {
        for{
            pipe <- stream.open()
            _ <- stream.drain(pipe)
            _ <- pipe.close()
        } yield ()
    }
}

我决定放弃 Close[F[_], T] 的原因是我的应用程序中的某些 Pipes 本质上是不可关闭的,所以放起来有点奇怪 所有 Pipes

的关闭方法

这是我得到的错误:

Error:(32, 4) @typeclass may only be applied to types that take a single type parameter
  @typeclass trait Close[F[_], T]

问题: 如果 trait 有多个类型参数(如 Close[F[_], T]),我是否我必须自己编写所有 Ops/Syntax 样板文件,而 symulacrum 的 @typeclass 对此无能为力?

靠自己。

https://github.com/mpilquist/simulacrum#known-limitations

Known Limitations

  • Only type classes that abstract over a proper type or a unary type constructor are currently supported.