我创建了一个简单的trait
及其实现:
trait UserRepositoryAlg[F[_]] {
def find(nick: String): F[User]
def update(user: User): F[User]
}
class UserRepositoryInterpreter extends UserRepositoryAlg[Either[Error,*]] {
override def find(nick: String): Either[Error,User] = for {
res <- users.find(user => user.nick == nick).toRight(UserError)
} yield res
override def update(user: User): Either[Error,User] = for {
found <- users.find(u => u.nick == user.nick).toRight(UserError)
updated = found.copy(points = found.points + user.points)
} yield updated
}
在这里,我想使用Either
或EitherT
来“捕获”错误,但是我也想使用IO
或Future
作为主要的monad。在主类中,我创建了对此实现的调用:
object Main extends App {
class Pointer[F[_] : Monad](repo: UserRepositoryAlg[F]) {
def addPoints(nick: String): EitherT[F,Error,User] = {
for {
user <- EitherT.right(repo.find(nick))
updated <- EitherT.right(repo.update(user))
} yield Right(updated)
}
}
val pointer = new Pointer[IO](new UserRepositoryInterpreter{}).addPoints("nick")
}
但是在创建pointer
的行中,IntelliJ向我显示了一个错误:Type mismatch - required: UserRepositoryAlg[F],found: UserRepositoryInterpreter
,但我不明白为什么。我使用Pointer
作为F[_]
创建了IO
类,并想使用UserRepositoryAlg[F]
的实现。如何解决此问题,或者在这种情况下有什么好的做法?如果我想实现以下目标:IO[Either[Error,User]]
或EitherT[IO,User]
。
我试图将class UserRepositoryInterpreter extends UserRepositoryAlg[Either[Error,*]]
改成class UserRepositoryInterpreter[F[_]] extends UserRepositoryAlg[F[Either[Error,*]]]
之类的东西,但这无济于事。
编辑:
我发现了如何使用F[Either[Error,User]]
来转换Applicative[F]
来返回A => F[A]
:
class UserRepositoryInterpreter[F[_] : Applicative] extends UserRepositoryAlg[F[Either[Error,*]]] {
override def find(nick: String): F[Either[Error,User]] = for {
res <- Applicative[F].pure(users.find(user => user.nick == nick).toRight(UserError))
} yield res
override def update(user: User): F[Either[Error,User]] = for {
found <- Applicative[F].pure(users.find(u => u.nick == user.nick).toRight(UserError))
updated = Applicative[F].pure(found.map(u => u.copy(points = u.points + user.points)))
} yield updated
}
但是我的主要功能仍然有问题,因为我无法获得Right
的{{1}}值:
Either
这里 def addPoints(nick: String): EitherT[F,User] = {
for {
user <- EitherT.liftF(repo.find(nick))
updated <- EitherT.rightT(repo.update(user))
} yield Right(updated)
}
updated <- EitherT.rightT(repo.update(user))
是user
,但我只需要通过Either[Error,User]
。所以我尝试做类似的事情:
User
并通过它,但这也无济于事。我应该如何看待这个价值?