2

I have defined the following object which I want to sort a string concurrently.

object QuoteFuture extends App {

  val wordsStr : String = "Lorem ipsum dolor ..."

  import scala.concurrent._
  import ExecutionContext.Implicits.global

  val f : Future[String] = Future {
    val wordsArr = wordsStr.split("\\s+")
    wordsArr.sorted // <<<<< Compiler error from here (line 15)
  }
  f.onComplete(t => {
    if (t.isSuccess) {
      println(s"words sorted: ${t.get}")
    } else {
      println("could not sort words")
    }
  })
}

I have omitted the entire string for brevity. When I attempt to run the code above, I get the following compilation error:

Error:(15, 14) polymorphic expression cannot be instantiated to expected type;
 found   : [B >: String]Array[String]
 required: String
    wordsArr.sorted

I have no idea what this means.

1 Answer 1

4

You've declared your future f to be of type String, but wordsArr.sorted is going to be an Array[String], so you should declare it as

val f : Future[Array[String]] = //...
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.