Skip to content

Instantly share code, notes, and snippets.

@etorreborre
Created September 16, 2020 07:41
Show Gist options
  • Save etorreborre/4ee9bbbbc2b160187990efa0c9868f93 to your computer and use it in GitHub Desktop.
Save etorreborre/4ee9bbbbc2b160187990efa0c9868f93 to your computer and use it in GitHub Desktop.
Deactivate implicits in traits scopes with Dotty
import scala.concurrent.ExecutionContext
// this type is used by the implicit search to succeed in finding Not[C]
// when C can not be found as an implicit value
import scala.implicits.Not
trait ImplicitExecutionContextFromExecutionEnv:
/**
* if an implicit execution environment is in scope, it can be used as an execution context
* Here we also expect that no DeactivateImplicitExecutionContext implicit value can be found
* by requiring Not[DeactivateImplicitExecutionContext]
*/
given executionEnvToExecutionContext(using ee: ExecutionEnv, not: Not[DeactivateImplicitExecutionContext]) as ExecutionContext =
ee.executionContext
/**
* deactivate the conversion between an implicit execution environment to an execution context
*/
// deactivation type
trait DeactivateImplicitExecutionContext
// this trait "deactivates" ImplicitExecutionContextFromExecutionEnv by providing an implicit value
// for the DeactivateImplicitExecutionContext type
trait NoImplicitExecutionContextFromExecutionEnv extends ImplicitExecutionContextFromExecutionEnv:
given deactivateImplicitExecutionContext as DeactivateImplicitExecutionContext =
new DeactivateImplicitExecutionContext {}
// EXAMPLE
object TryItOk extends ImplicitExecutionContextFromExecutionEnv {
given ee as ExecutionEnv = ???
// works fine
summon[ExecutionContext]
}
object TryItKo extends ImplicitExecutionContextFromExecutionEnv with NoImplicitExecutionContextFromExecutionEnv {
given ee as ExecutionEnv = ???
// fails with
// |Cannot find an implicit ExecutionContext. You might pass
// [error] |an (implicit ec: ExecutionContext) parameter to your method.
summon[ExecutionContext]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment