【问题标题】:Trait with Higher-Kinded Types具有高级类型的特征
【发布时间】:2015-04-06 14:04:46
【问题描述】:

鉴于以下trait(来自这个有用的shapeless talk):

scala> trait NatT[F[_], G[_]] { def apply[T](f: F[T]): G[T] }
warning: there were two feature warnings; re-run with -feature for details
defined trait NatT

我相信这意味着NatT 接受两个更高级的参数:FG

有了这个假设,我尝试创建一个 F 和 G 类型为 Option 的实例:

scala> case object Maybe extends NatT[Option, Option] {
     |   override def apply(f: Option[Int]) = f
     | }
<console>:8: error: object creation impossible, since method apply in trait NatT of type [T](f: Option[T])Option[T] is not de
fined
       case object Maybe extends NatT[Option, Option] {
                   ^
<console>:9: error: method apply overrides nothing.
Note: the super classes of object Maybe contain the following, non final members named apply:
def apply[T](f: Option[T]): Option[T]
         override def apply(f: Option[Int]) = f
                      ^

如何解决创建Maybe 实例的这种尝试?

【问题讨论】:

    标签: scala


    【解决方案1】:

    您的apply 方法缺少类型参数。就这么简单。

    case object Maybe extends NatT[Option, Option] {
        def apply[A](f: Option[A]): Option[A] = f
    }
    

    您尝试在没有类型参数的情况下定义apply 被视为不同的方法,因此apply 似乎未实现。鉴于 FG 应该是更高的种类,尝试将它们修复为 Option[Int] 并没有任何意义。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-12-11
      • 2017-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多