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)
}
}
muou0213
  • 1
  • 3

1 Answers1

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)
Arseniy Zhizhelev
  • 2,381
  • 17
  • 21
  • 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? – muou0213 Jan 05 '17 at 11:28
  • No, there are some natural limitations for infix notation. See https://www.scala-lang.org/files/archive/spec/2.11/06-expressions.html#function-applications, https://www.scala-lang.org/files/archive/spec/2.11/06-expressions.html#infix-operations. `Infix` doesn't take arguments. It's a single expression that is taken. (See also http://stackoverflow.com/questions/1181533/what-are-the-precise-rules-for-when-you-can-omit-parenthesis-dots-braces-f, and http://docs.scala-lang.org/style/method-invocation.html) – Arseniy Zhizhelev Jan 05 '17 at 11:43