gpt4 book ai didi

scala - scala.js 示例应用程序中的 "Cannot find an implicit ExecutionContext"错误。

转载 作者:行者123 更新时间:2023-12-03 11:45:37 25 4
gpt4 key购买 nike

下面是来自 Scala.js 电子书中的一个例子:

package webpage

import org.scalajs.dom.ext.Ajax

import scala.scalajs.js
import scala.scalajs.js.annotation.JSExport
import scalatags.JsDom.all._
import org.scalajs.dom
import dom.html

@JSExport
object HelloWorld1 extends {
@JSExport
def main(target: html.Div) = {

lazy val box = input(
`type`:="text",
placeholder:="Type here!"
).render

lazy val output = div(
height:="400px",
overflowY:="scroll"
).render

box.onkeyup = (e: dom.Event) => {
output.innerHTML = "Loading..."
fetchWeather(box.value)
}

target.appendChild(
div(
h1("Weather Search"),
p(
"Enter the name of a city to pull the ",
"latest weather data from api.openweathermap.com!"
),
p(box),
hr, output, hr
).render
)

def fetchWeather(query: String) = {
val searchUrl =
"http://api.openweathermap.org/data/" +
"2.5/find?type=like&mode=json&q=" +
query

for{
xhr <- Ajax.get(searchUrl)
if query == box.value
} js.JSON.parse(xhr.responseText).list match{
case jsonlist: js.Array[js.Dynamic] =>
output.innerHTML = ""
showResults(jsonlist, query)
case _ =>
output.innerHTML = "No Results"
}
}
def showResults(jsonlist: js.Array[js.Dynamic], query: String) = {
for (json <- jsonlist) {
val name = json.name.toString
val country = json.sys.country.toString
val weather = json.weather.pop().main.toString

def celsius(kelvins: js.Dynamic) = {
kelvins.asInstanceOf[Double] - 273.15
}.toInt

val min = celsius(json.main.temp_min)
val max = celsius(json.main.temp_max)
val humid = json.main.humidity.toString
val (first, last) = name.splitAt(query.length)
output.appendChild(
div(
b(span(first, backgroundColor:="yellow"), last, ", ", country),
ul(
li(b("Weather "), weather),
li(b("Temp "), min, " - ", max),
li(b("Humidity "), humid, "%")
)
).render
)
}
}
}
}

编译时出现错误:
[info] Compiling 1 Scala source to /Users/kaiyin/personal_config_bin_files/workspace/workbench-example-app/target/scala-2.11/classes...
[error] /Users/kaiyin/personal_config_bin_files/workspace/workbench-example-app/src/main/scala/HelloWorld1.scala:51: 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.
[error] if query == box.value
[error] ^
[warn] /Users/kaiyin/personal_config_bin_files/workspace/workbench-example-app/src/main/scala/HelloWorld1.scala:53: non-variable type argument scala.scalajs.js.Dynamic in type pattern scala.scalajs.js.Array[scala.scalajs.js.Dynamic] is unchecked since it is eliminated by erasure
[warn] case jsonlist: js.Array[js.Dynamic] =>
[warn] ^
[warn] one warning found
[error] one error found
[info] workbench: Checking example-fastopt.js
[info] workbench: Checking webpage-fastopt.js
[error] (compile:compile) Compilation failed
[error] Total time: 0 s, completed 7 sept. 2015 12:51:38
78. Waiting for source changes... (press enter to interrupt)

这里出了什么问题?

最佳答案

正如错误所暗示的那样,添加以下导入:

import scala.concurrent._
import ExecutionContext.Implicits.global

关于scala - scala.js 示例应用程序中的 "Cannot find an implicit ExecutionContext"错误。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32437585/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com