英文:
could not find implicit value for evidence parameter of type cats.MonadThrow[sttp.client3.Identity]
问题
I'm considering using stac4s as a STAC client in our Scala project but boy, is it giving me a hard time. The use of cats, monads and all that fancy stuff is very daunting and there's no documentation to speak of.
I'm stuck right at the start: instantiating the thing.
import com.azavea.stac4s.api.client.SttpStacClient
import org.junit.Test
import sttp.client3._
class Stac4sTest {
@Test
def testDrive(): Unit = {
val stacClient = SttpStacClient(client = HttpURLConnectionBackend(), baseUri = uri"") // <-- error here
}
}
This gives me this compile error:
error: could not find implicit value for evidence parameter of type cats.MonadThrow[sttp.client3.Identity]
I'm assuming it's something easy like a missing import but I haven't been able to find it.
This is stac4s version 0.8.1.
英文:
I'm considering using stac4s as a STAC client in our Scala project but boy, is it giving me a hard time. The use of cats, monads and all that fancy stuff is very daunting and there's no documentation to speak of.
I'm stuck right at the start: instantiating the thing.
<!-- language: scala -->
import com.azavea.stac4s.api.client.SttpStacClient
import org.junit.Test
import sttp.client3._
class Stac4sTest {
@Test
def testDrive(): Unit = {
val stacClient = SttpStacClient(client = HttpURLConnectionBackend(), baseUri = uri"") // <-- error here
}
}
This gives me this compile error:
> error: could not find implicit value for evidence parameter of type cats.MonadThrow[sttp.client3.Identity]
I'm assuming it's something easy like a missing import but I haven't been able to find it.
This is stac4s version 0.8.1.
答案1
得分: 1
我从未使用过stac4s库,但通常在实例化基于sttp和cats-effect的客户端时,您通常会得到一个IO
值,它描述整个程序,包括所有的副作用(在这里是分配客户端等)。因此,您需要在创建描述后对其进行评估。例如,遵循sttp的文档:
val program = HttpClientCatsBackend.resource[IO]().use { backend =>
val stacClient = SttpStacClient(client = backend, baseUri = uri"""")
// 其他操作
}
import cats.effect.unsafe.implicits.global
program.unsafeRunSync()
unsafeRunSync
执行程序,正如它所描述的那样,使用给定的线程池来处理任何分叉。
请注意,您无法使用HttpURLConnectionBackend()
,因为它是一个同步客户端,而stac似乎要求使用cats-effect的后端,可以参考这里。
英文:
I haven't ever used the stac4s library, but as far as instantiating an sttp+cats-effect based client, you typically end up with an IO
value, which describes the whole program, with all of its side effects (here, allocating the client etc.). So you need to evaluate it after the description is created. For example, following sttp's docs:
val program = HttpClientCatsBackend.resource[IO]().use { backend =>
val stacClient = SttpStacClient(client = backend, baseUri = uri"")
// other operations
}
import cats.effect.unsafe.implicits.global
program.unsafeRunSync()
The unsafeRunSync
executes the program, as it is described, using the given thread pool for any forks.
Note that you weren't able to use HttpURLConnectionBackend()
, as it is a synchronous client, while stac seems to require a backend which uses cats-effect.
答案2
得分: -1
当处理这种类型的错误时,我通常建议查看库的测试,您会在其中找到如何实例化对象或缺少哪些导入。
例如,您可以从 https://github.com/azavea/stac4s/blob/master/modules/client/jvm/src/test/scala/com/azavea/stac4s/api/client/SttpStacClientSpec.scala 开始,这将引导您浏览其他类,并最终找到缺少什么。
您的IDE也可以帮助您建议缺少什么。
英文:
Not answering this specific case but some guidance
...
When dealing with such kind of errors, I often recommend to look at the tests of the library and you'll somehow find out how to instantiate things or what import is missing.
For instance, you can start by looking at https://github.com/azavea/stac4s/blob/master/modules/client/jvm/src/test/scala/com/azavea/stac4s/api/client/SttpStacClientSpec.scala which will guide you to other classes and ultimately find what's missing.
Your IDE can help as well to suggest what's missing.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论