为什么编译器找不到Implicits.global导入的隐式ExecutionContext?(Why can't the compiler find implicit ExecutionC

系统教程 行业动态 更新时间:2024-06-14 16:53:07
为什么编译器找不到Implicits.global导入的隐式ExecutionContext?(Why can't the compiler find implicit ExecutionContext with Implicits.global imported?)

我有以下代码:

import java.util.concurrent.Executor import scala.concurrent.Future trait Storage { def store(location: String, content: Array[Byte])(implicit exec: Executor): Future[String] } object LocalStorage extends Storage { override def store(location: String, content: Array[Byte])(implicit exec: Executor): Future[String] = Future { ... do some stuff ... "Hello" } }

以下是测试代码:

object LocalStorageTest extends App{ import scala.concurrent.ExecutionContext.Implicits.global val testImage = AsyncWebClient get "some url" val result = testImage.flatMap{content => LocalStorage.store("logo.png",content)} val status =Await.result(result, 30 seconds) println(status) AsyncWebClient.shutDown }

每当我尝试运行代码时,我都会收到以下错误:

找不到隐式ExecutionContext。

为什么? 范围内是不是scala.concurrent.ExecutionContext.Implicits.global? 当此导入直接添加到LocalStorage对象时,它可以工作(!)

但我失去了改变背景的可能性。 因为我不希望每次都在全局运行此代码。 由于这是akka应用程序和“生产”运行时的一部分,因此应该在某个调度程序上运行。 仅用于测试我想在全局执行上下文中运行它。

我在这里错过了一些重要的概念,还是我的设计错了,我失去了这种灵活性? 这个问题在哪里?!

I have the following piece of code:

import java.util.concurrent.Executor import scala.concurrent.Future trait Storage { def store(location: String, content: Array[Byte])(implicit exec: Executor): Future[String] } object LocalStorage extends Storage { override def store(location: String, content: Array[Byte])(implicit exec: Executor): Future[String] = Future { ... do some stuff ... "Hello" } }

And here comes the code for testing:

object LocalStorageTest extends App{ import scala.concurrent.ExecutionContext.Implicits.global val testImage = AsyncWebClient get "some url" val result = testImage.flatMap{content => LocalStorage.store("logo.png",content)} val status =Await.result(result, 30 seconds) println(status) AsyncWebClient.shutDown }

Whenever I try to run the code, I am getting the following error:

Cannot find an implicit ExecutionContext.

Why? Isn't scala.concurrent.ExecutionContext.Implicits.global in scope already? When this import is added directly to LocalStorage object, it works (!)

BUT I am loosing a possibility to alter the context. Since I do not want to run this code on global every time. As this is a part of an akka application and for "production" runtime it is supposed to run on some dispatcher. Just for testing I would like to run it on a global execution context.

Am I missing some important concept here or is my design wrong that I am loosing such flexibility? Where is the issue?!

最满意答案

ExecutionContext与Executor 。 你应该让方法接受一个implicit exec: ExecutionContext ( scala.concurrent.ExecutionContext )然后它将按你的预期运行。

An ExecutionContext is not the same thing as an Executor. You should make the method accept an implicit exec: ExecutionContext (scala.concurrent.ExecutionContext) and then it will behave as you expect.

更多推荐

本文发布于:2023-04-05 21:06:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/1e84d3884c3b8d7cd9fac08f37e5850f.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:找不到   编译器   隐式   Implicits   implicit

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!