Scala, для понимания и EitherT - PullRequest
0 голосов
/ 01 ноября 2018

Мой стек технологий

  • Play Framework 2.6
  • Scala 2.12.6
  • Play-Slick 3.0.0
  • Кошки 1.4.0

Я пытаюсь добиться этой функции, чтобы обновить строку в базе данных

def update(userId: Long, user: User) = {
  (for {
    _ <- EitherT(updateUser(userId, user))
    user: User <- EitherT(findById(userId))
    userProfile: userProfile <- EitherT(userProfileRepository.findById(user.userProfileId))
  } yield (user, userProfile).map {
    case (user: User, userProfile: UserProfile) =>
      val response = new UserResponse(user, userProfile)
      Right(response)
    case error =>
      val str = s"update failure: $error"
      Left(str)
  }
}

но когда я пытаюсь скомпилировать этот код с помощью EitherT, я получаю

значение withFilter не является членом cats.data.EitherT

1 Ответ

0 голосов
/ 01 ноября 2018

Вы пытаетесь сопоставить с шаблоном внутри для понимания (хотя это выглядит только как невинное объявление типа). Но для сопоставления с образцом внутри для понимания требуется реализация withFilter (подумайте, что должно произойти, если сопоставление с образцом не удастся?). Так что удалите сопоставление по типам, и оно должно работать:

def update(userId: Long, user: User) = {
  (for {
    _ <- EitherT(updateUser(userId, user))
    user <- EitherT(findById(userId))
    userProfile <- EitherT(userProfileRepository.findById(user.userProfileId))
  } yield (user, userProfile).map {
    case (user: User, userProfile: UserProfile) =>
      val response = new UserResponse(user, userProfile)
      Right(response)
    case error =>
      val str = s"update failure: $error"
      Left(str)
  }
}
...