我创建了一个简单的trait
和服务:
@finalAlg
@autoFunctorK(true)
trait BettingService[F[_]] {
def put(bet: Bet): F[Bet]
}
class BettingServiceMock[F[_] : Async] extends BettingService[F] {
override def put(bet: Bet): F[Bet] = {
val id = randomUUID().toString
for {
created <- Bet(BetId(id), bet.stake, bet.name)
} yield created
}
}
Bet
和BetId
是case classes
:
case class Bet(betId: BetId, stake: BigDecimal, name: String)
case class BetId(betId: String) extends AnyVal
运行该程序时,出现错误:Error:(12, 21) value map is not a member of model.Bet <- Bet(BetId(id), bet.stake, bet.name)
对我来说很奇怪-为什么我不能从for-comprehension
返回好的类型?这样的想法是返回与给定参数相同的对象,但是具有随机ID。
我以为也许我需要新的Bet
类的实例,但是后来我无法像我想的那样将其返回为F[_]
。
答案 0 :(得分:3)
reconnectInterval
的类型为Bet(BetId(id), bet.stake, bet.name)
,但预期类型为Bet
。
尝试
F[Bet]
或
import cats.syntax.functor._
for {
created <- Applicative[F].pure(Bet(BetId(id), bet.stake, bet.name))
} yield created
或
import cats.syntax.functor._
import cats.syntax.applicative._
for {
created <- Bet(BetId(id), bet.stake, bet.name).pure
} yield created
或
Applicative[F].pure(Bet(BetId(id), bet.stake, bet.name))