0

I'm developing a project with ScalaFX and MySQL database.

SBT successfully added MySQL connector via build.sbt file. When it compiles the project, it stops with a type mismatch error:

[error]  found   : com.aitrich.scalafx.test.DbConnection.type (with underlying type object com.aitrich.scalafx.test.DbConnection)
[error]  required: com.aitrich.scalafx.test.DbConnection
[error]     val orders: Seq[Person] = OrderDao.getAllOrders(dbc)
[error]                                                     ^
[error] one error found
[error] (compile:compile) Compilation failed
[error] Total time: 14 s, completed Nov 14, 2013 12:04:06 PM

The following is a code snippet from the main method:

var dbc = DbConnection
val orders: Seq[Person] = OrderDao.getAllOrders(dbc)

This is the DbConnection case class:

case class DbConnection() {
  def getConnectionString = 
    "jdbc:mysql://%s:3306/simpleorder?user=%root&password=%sa".
      format("localhost","root","sa")
}

Why does compile fail?

4

1 回答 1

1

tl; dr您需要实例化(创建实例)DbConnection案例类。

这绝不是 SBT 或 ScalaFX 的问题。

您作为参数传递给OrderDao.getAllOrders方法的是类型而不是类型的实例。类型根本不匹配,Scala 编译器会中断编译(这正是首先使用 Scala 的原因——在编译时进行彻底的类型检查)。

换行

var dbc = DbConnection

var dbc = new DbConnection

并且编译器通过了该行。注意new关键字。

于 2014-01-02T12:13:36.043 回答