我创建了一个简单的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
orEitherT
来“捕捉”错误,但我也想使用IO
orFuture
作为主要的单子。在我的主类中,我创建了对此实现的调用:
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
我不明白为什么。我用as 创建了一个Pointer
类,并想使用. 我该如何解决这个问题或者在这种情况下有什么好的做法?如果我想实现这样的目标:或.F[_]
IO
UserRepositoryAlg[F]
IO[Either[Error, User]]
EitherT[IO, Error, User]
我试图改变class UserRepositoryInterpreter extends UserRepositoryAlg[Either[Error, *]]
成类似的东西class UserRepositoryInterpreter[F[_]] extends UserRepositoryAlg[F[Either[Error, *]]]
,但它对我没有帮助。
编辑:
我发现了如何F[Either[Error,User]]
使用Applicative[F]
which transform返回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
}
但是我在 main 函数中仍然存在问题,因为我无法获得以下Right
值Either
:
def addPoints(nick: String): EitherT[F, Error, 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
。所以我尝试做类似的事情:
Right(user).map(u=>u)
并通过它,但它也无济于事。我应该如何取这个值?