0
我有一個id
這是長。但如果我getAs[String]
,它盲目地返回Some(0)
而不是無。它檢查密鑰的存在而不是類型。卡斯巴的類型不安全行爲
scala> collection
res26: com.mongodb.DBObject = { "_id" : { "$oid" : "520f8bf544ae41ec63d02eec"} , "date_about" : "2013-08-17T20:13:00.365Z" , "date_created" : "2013-08-17T20:13:00.365Z" , "date_modified" : "2013-09-07T18:03:20.101Z" , "id" : 0 , "node_type" : "meta-folder" , "parent_id" : 0 , "title" : "my stuff, renamed by ajax, wow" , "version" : 2}
scala> collection.getAs[String]("id")
res27: Option[String] = Some(0)
scala> collection.getAs[Long]("id")
res28: Option[Long] = Some(0) //get-ing this is an java.lang.ClassCastException
scala> collection.getAs[Long]("id").get
res29: Long = 0
這是不是意料之外的行爲;我應該沒有得到Option[String] = None
?我如何得到預期的行爲?我sbt-ed「org.mongodb」%%「casbah」%「2.6.2」