0

When I run the following code, I get some error:

Error:(15, 25) missing parameter type for expanded function
The argument types of an anonymous function must be fully known. (SLS 8.5)
Expected type was: ?
  testFuture onComplete({
                    ^

code:

object TestFuture extends App{

val exe = ExecutionContext.fromExecutor(Executors.newCachedThreadPool())

testFuture onComplete({
case Success((str,i)) =>{
  println(str,i)
}
case Failure(e) =>{
  e.printStackTrace()
}})(exe)


println(Runtime.getRuntime.availableProcessors())
Thread.sleep(2000)

def testFuture:Future[(String,Int)] = Future[(String,Int)] {
  Thread.sleep(1000)
  ("oh my sky",12)
}(exe)
}

When I decorate 'val exe' with 'implicit' and call the currying function without explicitly using 'exe' like following code, it goes right. Can you tell me why?

object TestFuture extends App{
implicit val exe = ExecutionContext.fromExecutor(Executors.newCachedThreadPool())

testFuture onComplete({
case Success((str,i)) =>{
  println(str,i)
}
case Failure(e) =>{
  e.printStackTrace()
}})


println(Runtime.getRuntime.availableProcessors())
Thread.sleep(2000)

def testFuture:Future[(String,Int)] = Future[(String,Int)] {
  Thread.sleep(1000)
  ("oh my sky",12)
}
}

1 Answer 1

1

I guess, infix method invocation doesn't support multiple argument lists. Try to use dot-notation:

testFuture.onComplete{
  case Success((str,i)) =>
    println(s"$str, $i")
  case Failure(e) =>
    e.printStackTrace()
}(exe)
Sign up to request clarification or add additional context in comments.

2 Comments

yes,you are right. When I modify my code as you said, it goes right. But can you tell me the reason, aren't the 'space' and 'dot-notation' the same in Scala?
No, there are some natural limitations for infix notation. See scala-lang.org/files/archive/spec/2.11/…, scala-lang.org/files/archive/spec/2.11/…. Infix doesn't take arguments. It's a single expression that is taken. (See also stackoverflow.com/questions/1181533/…, and docs.scala-lang.org/style/method-invocation.html)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.