简体   繁体   English

在SBT中使用ScalaFX编译项目时出现类型不匹配错误

[英]Type mismatch error upon compiling project with ScalaFX in SBT

I'm developing a project with ScalaFX and MySQL database. 我正在用ScalaFX和MySQL数据库开发一个项目。

SBT successfully added MySQL connector via build.sbt file. SBT通过build.sbt文件成功添加了MySQL连接器。 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: 以下是main方法的代码片段:

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

This is the DbConnection case class: 这是DbConnection案例类:

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

Why does compile fail? 为什么compile失败?

tl;dr You need to instantiate (create an instance of) DbConnection case class. tl; dr您需要实例化(创建实例) DbConnection案例类。

It's in no way SBT's or ScalaFX's issue. 绝不是SBT或ScalaFX的问题。

What you pass as an argument to OrderDao.getAllOrders method is a type not an instance of a type. 您作为参数传递给OrderDao.getAllOrders方法的是类型,而不是类型的实例。 The types simply don't match and the Scala compiler breaks compilation (that's exactly the reason to use Scala in the first place - a thorough type checking at compile time). 类型根本不匹配,Scala编译器中断了编译(这正是首先使用Scala的原因-编译时进行彻底的类型检查)。

Change the line 换线

var dbc = DbConnection

to

var dbc = new DbConnection

and the compiler gets pass that line. 然后编译器通过了这一行。 Note the new keyword. 注意new关键字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM