[2.5.6 Scala] ambiguous implicit values if using execution context

1,688 views
Skip to first unread message

Jens Grassel

unread,
Sep 12, 2016, 7:58:47 AM9/12/16
to Play Framework
Hi,

I've created a helper function (not an action) within a controller that takes an implicit execution context like this:

def resolveUser(id: Id)(implicit context: ExecutionContext): Future[Option[User]] = authDAO.findAccountById(id)

Also there are actions that use futures like this:

def fancy = AsyncStack(AuthorityKey ->Blabla) {
 
implicit request =>
   
for {
      a
<- someFutureStuff
      b
<- anotherFuture
   
} yield { ... }
}

If I try to compile this then I get the expected compiler errors about a missing execution context for the actions using futures:

[error] ...: Cannot find an implicit ExecutionContext. You might pass
[error] an (implicit ec: ExecutionContext) parameter to your method
[error] or import scala.concurrent.ExecutionContext.Implicits.global.

However if I add import play.api.libs.concurrent.Execution.Implicits._ then I get an error about ambiguous implicit values for the execution context:


[error] ...: ambiguous implicit values:
[error]  both method defaultContext in object Implicits of type => scala.concurrent.ExecutionContext
[error]  and value context of type scala.concurrent.ExecutionContext
[error]  match expected type scala.concurrent.ExecutionContext
[error]   def resolveUser(id: Id)(implicit context: ExecutionContext): Future[Option[User]] = authDAO.findAccountById(id)
[error]                                                                                                  
                     ^

Has anyone ever experienced this behaviour and an idea how to solve this?

Regards,

Jens

Mike Nielsen

unread,
Sep 12, 2016, 10:44:14 AM9/12/16
to play-fr...@googlegroups.com
Where do you add the import? Try it inside your action definition maybe?

Sent from my iPhone
--
You received this message because you are subscribed to the Google Groups "Play Framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to play-framewor...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/play-framework/1fcbf223-96ef-4d9b-9123-168ce196add0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Marius Soutier

unread,
Sep 12, 2016, 10:44:14 AM9/12/16
to play-fr...@googlegroups.com
The error message is pretty clear, you can't have an implicit method parameter AND an implicit imported value of the same type within the same scope. So you have to change the scoping.

If you are importing the EC in the controller, you could remove the parameter from the helper method. Alternatively extract the method into a trait.


Message has been deleted
Message has been deleted

Will Sargent

unread,
Sep 12, 2016, 2:19:11 PM9/12/16
to play-fr...@googlegroups.com
You have an implicit execution context that is passed in both as a parameter, and as an import, and it does not know how to resolve both.

The best way to resolve this to use Play's implicit execution context, which can be done by constructor.  Here, I'll show where there's a DI injection on MessagesAPI already:

class MyController @Inject() (val messagesApi: MessagesApi)(implicit ec: ExecutionContext) extends Controller with I18nSupport {


}

This should be all you need if you are using an execution context that is CPU bound -- if you are resolving a user and passing that execution context to a blocking API, then you will want to bind a custom execution context that is configured with a ThreadPoolExecutor and a larger number of threads.


Please see UserDAOExecutionContext as an example of a custom execution context.




--
Will Sargent
Engineer, Lightbend, Inc.


To unsubscribe from this group and stop receiving emails from it, send an email to play-framework+unsubscribe@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Play Framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to play-framework+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/play-framework/2961CDC9-59D3-46AE-8825-1CB52D36F61F%40xichar.com.

Sushmita Pal

unread,
Jan 24, 2018, 10:28:19 AM1/24/18
to Play Framework

Hi, 
I am using scala-stm-2.11-0.7 jar and when I define View of type Tmap it throws
"The method get(String) is ambiguous for the type TMap.View<String,ResourcePropertiesWrapperForTxnMap>" error with jdk 1.8 while it work fine with jdk 1.7. How to fix this?
Reply all
Reply to author
Forward
0 new messages