You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

调用三个返回Future[Either[String,Int]]的方法时出现编译错误

Fixing the Compilation Error in Your evaluate Method

Let's break down what's likely causing the compilation error and walk through the correct way to implement your evaluate function.

First, here are your original methods for reference:

import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global

def greater(x:Int): Future[Either[String,Int]] = Future { if (x>0) Right(x) else Left("Is not greater") }
def less(x:Int): Future[Either[String,Int]] = Future { if (x<0) Right(x) else Left("Is not less") }
def zero(x:Int): Future[Either[String,Int]] = Future { if (x==0) Right(x) else Left("Is not zero") }

Common Causes of the Compilation Error

Most likely, your initial evaluate implementation is making one of these mistakes:

  • Returning multiple Futures instead of a single one: You can't just call all three methods in sequence and expect Scala to know which result to return.
  • Not properly unwrapping the nested Future[Either[...]] type: You need to use flatMap or map to handle the asynchronous Future, then pattern-match on the Either to handle success/failure cases.
  • Mismatched return types: If your evaluate declares a return type of Future[Either[String, Int]] but your implementation returns something else (like Either[String, Future[Int]]), you'll get a compilation error.

Correct Implementations

Option 1: Sequentially Check Conditions (First Match Wins)

This implementation tries each method in order, returning the first successful Right result. If all prior checks fail, it falls back to the last method:

def evaluate(x: Int): Future[Either[String, Int]] = {
  greater(x).flatMap {
    case success @ Right(_) => Future.successful(success)
    case Left(_) => less(x).flatMap {
      case success @ Right(_) => Future.successful(success)
      case Left(_) => zero(x)
    }
  }
}

Or using a for-comprehension for cleaner readability:

def evaluate(x: Int): Future[Either[String, Int]] = for {
  greaterResult <- greater(x)
  finalResult <- greaterResult match {
    case r @ Right(_) => Future.successful(r)
    case Left(_) => less(x).flatMap {
      case r @ Right(_) => Future.successful(r)
      case Left(_) => zero(x)
    }
  }
} yield finalResult

Option 2: Parallel Check (Return First Successful Result)

If you want to run all three checks in parallel and return the first one that succeeds (since a number can only be positive, negative, or zero, this will still only return one valid result):

def evaluate(x: Int): Future[Either[String, Int]] = {
  val allChecks = List(greater(x), less(x), zero(x))
  
  // Find the first Future that resolves to a Right
  Future.find(allChecks) {
    case Right(_) => true
    case Left(_) => false
  }.map {
    // Since every integer is positive, negative, or zero, this None case is theoretically unreachable
    case Some(success) => success
    case None => Left("No valid condition matched (impossible for integers)")
  }
}

Key Takeaways

  • When working with Future[Either[E, A]], always use flatMap to handle the asynchronous Future before pattern-matching on the Either.
  • Ensure your implementation's return type exactly matches what you've declared for evaluate (no mixing up Future[Either[...]] with Either[Future[...]]).
  • Use for-comprehensions to make asynchronous control flow easier to read and maintain.

内容的提问来源于stack exchange,提问作者ps0604

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 11:15:55