【发布时间】:2015-06-02 09:54:25
【问题描述】:
我正在尝试将以下代码从类中提取到特征中以供重用:
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 属性。
我怎样才能知道我应该在以下 trait 中给 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,但这并不重要。我想知道如何在隐式转换后找出值的类型。
【问题讨论】: