相关疑难解决方法(0)

多态方法适用于 lambda 类型,但不适用于 Scala 3 中的类型通配符

在 Scala 3 中,我可以使用 lambda 类型为状态定义一个函子:

  given stateFunctor[S]: Functor[[A] =>> State[S, A]] with
    override def map[A, B](a: State[S, A])(fx: A => B): State[S, B] = State(a.run.andThen { case (s, a) => (s, fx(a)) })
Run Code Online (Sandbox Code Playgroud)

我希望它能与?_通配符一起使用:

  given stateFunctor[S]: Functor[State[S, ?]] with
    override def map[A, B](a: State[S, A])(fx: A => B): State[S, B] = State(a.run.andThen { case (s, a) => (s, fx(a)) })
Run Code Online (Sandbox Code Playgroud)

但我收到以下编译错误:

类型参数domain.State[S, ? <: AnyKind] 与其绑定 [_$1] 的类型不同,给定 stateFunctor[S]: Functor[State[S, ? <: AnyKind]] 与

为什么不起作用?我缺少什么?我认为 …

scala higher-kinded-types kind-projector scala-3

2
推荐指数
1
解决办法
214
查看次数