如何找到已隐式转换的值的类型?
How to find the type of a value that has been implicitly converted?
我正在尝试将以下代码从 class 中提取到特征中以供重用:
import org.slf4j.{LoggerFactory, Logger}
import slick.driver.H2Driver.api._
import scala.concurrent.Await
import scala.concurrent.duration.Duration
object UserProfileFixtures {
val logger: Logger = LoggerFactory.getLogger(UserProfileFixtures.getClass)
val table = UserProfileQueries.query
// todo: Create a trait for all this
def createSchema(db: Database) = {
logger.info("Creating schema for the UserProfiles table")
Await.result(db.run((table.schema).create), Duration.Inf)
logger.info("UserProfiles table schema created")
}
}
问题是 table
被隐式转换为添加 schema
属性 的内容。如果我只是提升和移动上面的内容,table
上的隐式转换不会发生,并且编译器找不到 schema
属性.
如何找出我应该在以下特征中赋予 table
什么类型?
import org.slf4j.Logger
import slick.driver.H2Driver.api._
import scala.concurrent.Await
import scala.concurrent.duration.Duration
trait FixtureHelper {
val logger: Logger
val data: Seq
val table: TableQuery[_] // this type is wrong...
def createSchema(db: Database) = {
logger.info("Creating schema")
// compiler can't resolve `schema` in the line below
Await.result(db.run(table.schema.create), Duration.Inf)
logger.info("Schema created")
}
}
我使用的是 slick 3.0 BTW,但应该不会有什么不同。我想知道如何在隐式转换后找出值的类型。
您可以使用结构类型来获得结果隐式 class:
scala> implicit class RchStr(s: String) { def v = 0 }
defined class RchStr
scala> implicitly[{def v: Int}]("aaa")
res5: AnyRef{def v: Int} = RchStr@3ab71d5e //"RchStr" is implicitly inferred type here
scala> implicitly[{def v: Any}]("aaa")
res6: AnyRef{def v: Any} = RchStr@2de743a3 // you may not know type of `v` - just specify `Any` then
scala> implicitly[{def z: Any}]("aaa") //there is no implicit conversions to something which has `z` member
<console>:9: error: type mismatch;
found : String("aaa")
required: AnyRef{def z: Any}
implicitly[{def z: Any}]("aaa")
^
在这里,我需要使用方法 {def v: Int}
对任何内容进行一些隐式转换。在您的特定情况下,它应该类似于:
println(implicitly[{def schema: Any}](table).getClass())
如果您需要找出 table
的初始类型,您可以使用 table.getClass
或检查 scaladoc 以隐式推断 table
的隐式转换类型。
IntelliJ IDEA 还会显式显示(Cntrl + 鼠标悬停)推断类型。您可能还需要检查推断类型的一些超类型。
也可能有帮助:Showing inferred types of Scala expressions, Inferred type in a Scala program
这将为您提供确切类型的类型标记,可以隐式转换为 schema
:
import scala.reflect.runtime.universe._
def typeOf[T](x:T)( implicit tag: TypeTag[T], conversion: T => {def schema: Any} ) = tag
println(typeOf(table))
我正在尝试将以下代码从 class 中提取到特征中以供重用:
import org.slf4j.{LoggerFactory, Logger}
import slick.driver.H2Driver.api._
import scala.concurrent.Await
import scala.concurrent.duration.Duration
object UserProfileFixtures {
val logger: Logger = LoggerFactory.getLogger(UserProfileFixtures.getClass)
val table = UserProfileQueries.query
// todo: Create a trait for all this
def createSchema(db: Database) = {
logger.info("Creating schema for the UserProfiles table")
Await.result(db.run((table.schema).create), Duration.Inf)
logger.info("UserProfiles table schema created")
}
}
问题是 table
被隐式转换为添加 schema
属性 的内容。如果我只是提升和移动上面的内容,table
上的隐式转换不会发生,并且编译器找不到 schema
属性.
如何找出我应该在以下特征中赋予 table
什么类型?
import org.slf4j.Logger
import slick.driver.H2Driver.api._
import scala.concurrent.Await
import scala.concurrent.duration.Duration
trait FixtureHelper {
val logger: Logger
val data: Seq
val table: TableQuery[_] // this type is wrong...
def createSchema(db: Database) = {
logger.info("Creating schema")
// compiler can't resolve `schema` in the line below
Await.result(db.run(table.schema.create), Duration.Inf)
logger.info("Schema created")
}
}
我使用的是 slick 3.0 BTW,但应该不会有什么不同。我想知道如何在隐式转换后找出值的类型。
您可以使用结构类型来获得结果隐式 class:
scala> implicit class RchStr(s: String) { def v = 0 }
defined class RchStr
scala> implicitly[{def v: Int}]("aaa")
res5: AnyRef{def v: Int} = RchStr@3ab71d5e //"RchStr" is implicitly inferred type here
scala> implicitly[{def v: Any}]("aaa")
res6: AnyRef{def v: Any} = RchStr@2de743a3 // you may not know type of `v` - just specify `Any` then
scala> implicitly[{def z: Any}]("aaa") //there is no implicit conversions to something which has `z` member
<console>:9: error: type mismatch;
found : String("aaa")
required: AnyRef{def z: Any}
implicitly[{def z: Any}]("aaa")
^
在这里,我需要使用方法 {def v: Int}
对任何内容进行一些隐式转换。在您的特定情况下,它应该类似于:
println(implicitly[{def schema: Any}](table).getClass())
如果您需要找出 table
的初始类型,您可以使用 table.getClass
或检查 scaladoc 以隐式推断 table
的隐式转换类型。
IntelliJ IDEA 还会显式显示(Cntrl + 鼠标悬停)推断类型。您可能还需要检查推断类型的一些超类型。
也可能有帮助:Showing inferred types of Scala expressions, Inferred type in a Scala program
这将为您提供确切类型的类型标记,可以隐式转换为 schema
:
import scala.reflect.runtime.universe._
def typeOf[T](x:T)( implicit tag: TypeTag[T], conversion: T => {def schema: Any} ) = tag
println(typeOf(table))