Doobie 无法找到或构建类型 T 的 Read 实例
Doobie cannot find or construct a Read instance for type T
我正在使用 doobie 查询一些数据,一切正常,如下所示:
case class Usuario(var documento: String, var nombre: String, var contrasena: String)
def getUsuario(doc: String) =
sql"""SELECT documento, nombre, contrasena FROM "Usuario" WHERE "documento" = $doc"""
.query[Usuario]
.option
.transact(xa)
.unsafeRunSync()
但是如果我像这样声明一个具有类型限制的函数:
def getOption[T](f: Fragment): Option[T] = {
f.query[T]
.option
.transact(xa)
.unsafeRunSync()
}
我遇到了这些错误:
Error:(42, 12) Cannot find or construct a Read instance for type:
T
This can happen for a few reasons, but the most common case is that a data
member somewhere within this type doesn't have a Get instance in scope. Here are
some debugging hints:
- For Option types, ensure that a Read instance is in scope for the non-Option
version.
- For types you expect to map to a single column ensure that a Get instance is
in scope.
- For case classes, HLists, and shapeless records ensure that each element
has a Read instance in scope.
- Lather, rinse, repeat, recursively until you find the problematic bit.
You can check that an instance exists for Read in the REPL or in your code:
scala> Read[Foo]
and similarly with Get:
scala> Get[Foo]
And find the missing instance and construct it as needed. Refer to Chapter 12
of the book of doobie for more information.
f.query[T].option.transact(xa).unsafeRunSync()
Error:(42, 12) not enough arguments for method query: (implicit evidence: doobie.util.Read[T], implicit h: doobie.LogHandler)doobie.Query0[T].
Unspecified value parameter evidence.
f.query[T].option.transact(xa).unsafeRunSync()
有谁知道如何制作我想要的东西吗?我认为这是隐含的东西,但我不知道如何解决它。
为了 doobie 能够将 SQL 查询的结果转换为您的案例 class,它需要 [=12] 的实例=] typeclass 在范围内。
例如 Usuario
它需要 Read[Usuario]
的实例。幸运的是,doobie 能够为来自 typeclasses[ 的类型派生出 typeclasses =40=] 它已经知道了,比如 String
,所以在大多数情况下,我们不需要显式地创建这些。
在你的例子中,你想创建方法getOption
,它有类型参数T
,这意味着,编译器不知道哪个typeclass 要查找的类型。
您可以非常轻松地修复它,只需将 Read
的上下文绑定添加到您的类型(如 T: Read
或通过添加隐式参数)。这意味着当具体类型为T 应该已经知道了。
所以你的固定方法是:
def getOption[T: Read](f: Fragment): Option[T] = {
f.query[T]
.option
.transact(xa)
.unsafeRunSync()
或使用隐式参数:
def getOption[T](f: Fragment)(implicit read: Read[T]): Option[T] = {
f.query[T]
.option
.transact(xa)
.unsafeRunSync()
我正在使用 doobie 查询一些数据,一切正常,如下所示:
case class Usuario(var documento: String, var nombre: String, var contrasena: String)
def getUsuario(doc: String) =
sql"""SELECT documento, nombre, contrasena FROM "Usuario" WHERE "documento" = $doc"""
.query[Usuario]
.option
.transact(xa)
.unsafeRunSync()
但是如果我像这样声明一个具有类型限制的函数:
def getOption[T](f: Fragment): Option[T] = {
f.query[T]
.option
.transact(xa)
.unsafeRunSync()
}
我遇到了这些错误:
Error:(42, 12) Cannot find or construct a Read instance for type:
T
This can happen for a few reasons, but the most common case is that a data
member somewhere within this type doesn't have a Get instance in scope. Here are
some debugging hints:
- For Option types, ensure that a Read instance is in scope for the non-Option
version.
- For types you expect to map to a single column ensure that a Get instance is
in scope.
- For case classes, HLists, and shapeless records ensure that each element
has a Read instance in scope.
- Lather, rinse, repeat, recursively until you find the problematic bit.
You can check that an instance exists for Read in the REPL or in your code:
scala> Read[Foo]
and similarly with Get:
scala> Get[Foo]
And find the missing instance and construct it as needed. Refer to Chapter 12
of the book of doobie for more information.
f.query[T].option.transact(xa).unsafeRunSync()
Error:(42, 12) not enough arguments for method query: (implicit evidence: doobie.util.Read[T], implicit h: doobie.LogHandler)doobie.Query0[T].
Unspecified value parameter evidence.
f.query[T].option.transact(xa).unsafeRunSync()
有谁知道如何制作我想要的东西吗?我认为这是隐含的东西,但我不知道如何解决它。
为了 doobie 能够将 SQL 查询的结果转换为您的案例 class,它需要 [=12] 的实例=] typeclass 在范围内。
例如 Usuario
它需要 Read[Usuario]
的实例。幸运的是,doobie 能够为来自 typeclasses[ 的类型派生出 typeclasses =40=] 它已经知道了,比如 String
,所以在大多数情况下,我们不需要显式地创建这些。
在你的例子中,你想创建方法getOption
,它有类型参数T
,这意味着,编译器不知道哪个typeclass 要查找的类型。
您可以非常轻松地修复它,只需将 Read
的上下文绑定添加到您的类型(如 T: Read
或通过添加隐式参数)。这意味着当具体类型为T 应该已经知道了。
所以你的固定方法是:
def getOption[T: Read](f: Fragment): Option[T] = {
f.query[T]
.option
.transact(xa)
.unsafeRunSync()
或使用隐式参数:
def getOption[T](f: Fragment)(implicit read: Read[T]): Option[T] = {
f.query[T]
.option
.transact(xa)
.unsafeRunSync()