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

问题描述 投票:3回答:1

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

@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在这里无法解决?

scala functional-programming typeclass simulacrum
1个回答
4
投票

靠你自己。

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

已知限制

  • 目前仅支持通过适当类型或一元类型构造函数进行抽象的类型类。
© www.soinside.com 2019 - 2024. All rights reserved.