我正在尝试运行“Scala in Action”一书中的一些 scala 代码。以下是代码:
MongoClient.scala:
package com.scalainaction.mongo
import com.mongodb._
class MongoClient(val host: String, val port: Int){
require(host != null, "You have to provide a host name")
private val underlying = new Mongo(host, port)
def this() = this("127.0.0.1", 27017)
def version = underlying.getVersion
def dropDB(name: String) = underlying.dropDatabase(name)
def createDB(name: String) = DB(underlying.getDB(name))
def db(name: String) = DB(underlying.getDB(name))
}
DB.scala:
package com.scalainaction.mongo
import com.mongodb.{DB => MongoDB}
import scala.collection.convert.Wrappers._
class DB private(val underlying: MongoDB) {
def collectionName = for(name <- new
JSetWrapper(underlying.getCollectionNames)) yield name
}
object DB {
def apply(underlying: MongoDB) = new DB(underlying)
}
测试.scala:
package com.scalainaction.mongo
import com.scalainaction.mongo._
class test{
def client = new MongoClient
def db = client.createDB("mydb")
for (name <- db.collectionName) println(name)
}
编译后使用
scalac -cp /.../mongo-java-driver-2.11.3.jar MongoClient.scala DB.scala test.scala
我有:
MongoClient.scala:7: warning: constructor Mongo in class Mongo is deprecated: see corresponding Javadoc for more information.
private val underlying = new Mongo(host, port)
^
one warning found
看起来不错(?)。但是调用之后
scala -cp /.../mongo-java-driver-2.11.3.jar -deprecation test.scala
这显示了:
/.../test.scala:1: error: illegal start of definition
package com.scalainaction.mongo
^
one error found
我怎样才能运行test.scala没有错误?非常感谢你的帮助!