Search code examples
scalaimplicithigher-order-functions

Scala - how to pass implicit parameter into function (HOF)?


I have a function like this:

def getSomething: (String, Future[String]) => String = {
    case (name, surname) if (name == "Joe", surname.map(s => s == "Doe")) => "Is ok"
}

But compiler said he needs executionContext here in map function. I tried to do some magic with:

def getSomething (implicit e: ExecutionContext): (String, Future[String]) => String{...}

or

 def getSomething: (String, Future[String])(implicit e: ExecutionContext) => String{...}

But it not works. Is it possible to pass implicit parameter to function like this? Or could I do it in other way?


Solution

  • def getSomething (implicit e: ExecutionContext): (String, Future[String]) => String = ...
    

    should work.

    You can't write

    (String, Future[String]) => (implicit e: ExecutionContext) => String
    

    Implicit functions will appear in Scala 3.

    http://dotty.epfl.ch/docs/reference/contextual/context-functions.html

    http://dotty.epfl.ch/blog/2016/12/05/implicit-function-types.html