简体   繁体   中英

Scala,Currying on partial function parameter and implicit parameter

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)
}
}

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM