1

Dotty中给出以下内容:

object Domain {
  final case class Create(name: String) extends BaseCreate[Create] {
    override type Model = Domain
    override def service[F[_]](client: KeystoneClient[F]): CrudService[F, Domain, Create] = client.domains
  }
}
case class Domain(id: String)

class CrudService[F[_], Model, Create]
final class Domains[F[_]] extends CrudService[F, Domain, Domain.Create]

class KeystoneClient[F[_]] {
  val domains = new Domains[F]
}

trait BaseCreate[Create <: BaseCreate[Create]] {                                                                                                        
  type Model
  def service[F[_]](client: KeystoneClient[F]): CrudService[F, Model, Create]
}

我想“简化”BaseCreate这样我可以Create像这样实现:

final case class Create(name: String) extends BaseCreate.Aux[Domain, Create](_.domains)

我试过了:

object BaseCreate {
  abstract class Aux[M, Create](f: [F[_]] =>> KeystoneClient[F] => CrudService[F, M, Create]) extends BaseCreate[Create] {                                        
    type Model = M
    def service[F[_]](client: KeystoneClient[F]): CrudService[F, Model, Create] = f(client)
  }
}

但是编译器抱怨:

Missing type parameter for [F[_$5]] =>> KeystoneClient[F] => CrudService[F, M, Create]

这对我来说没有意义。什么是正确的语法,如果有的话?

使用此代码链接到 scastie

PS:我不想再向BaseCreate. 尤其是F因为这意味着该类Domain.Create将需要final case class Create[F[_]]完全不是预期的解决方案。

4

1 回答 1

5

我猜您将类型 lambda多态函数混淆了。

类型 lambda 将类型映射到类型,多态函数将值映射到值,只是其参数的类型可以变化。

对于类型 lambda,您应该使用=>>,对于多态函数,您应该使用普通的=>(两次即 ie [A] => (a: A) => f(a))。

所以应该是

object BaseCreate {
  abstract class Aux[M, Create](f: [F[_]] => KeystoneClient[F] => CrudService[F, M, Create]) extends BaseCreate[Create] {
    type Model = M
    def service[F[_]](client: KeystoneClient[F]): CrudService[F, Model, Create] = f(client)
  }
}
   
final case class Create(name: String) extends BaseCreate.Aux[Domain, Create](
  [F[_]] => (client: KeystoneClient[F]) => client.domains
)

但它不会在 Dotty 0.28.0-bin-20200920-e99793e-NIGHTLY 中编译错误

Found:    Object with PolyFunction {...}
Required: PolyFunction{
  apply: 
    [F[_$6]]
      (x$1: App.KeystoneClient[F]): 
        App.CrudService[F, App.Domain, App.Create]
}
    [F[_]] => (client: KeystoneClient[F]) => client.domains

似乎是一个错误。

于 2020-09-21T14:53:56.367 回答